Kor*_*jov 2 javascript jsx react-native react-props
我需要知道如何将 props 的默认值设置为 React Native。我有以下代码:
<View style={{ ...styles.textBox, ...props.bg }}>
<Text style={{ ...styles.pretitle, ...props.style }}>{props.pretitle}</Text>
<Text style={{ ...styles.title, ...props.style2 }}>{props.title}</Text>
<Text style={styles.introduction}>{{ '', ...props.introduction }}</Text>
</View>
Run Code Online (Sandbox Code Playgroud)
如果props.introduction是0我想将它设置为空,如:“”,或者有一些其他的价值,比如:“未设置”。
谢谢
你可以在 React 中使用defaultProps
export default class Example extends React.Component {
render() {
return (
<View>
<Text>{this.props.fName},{this.props.lName}</Text>
</View>
);
}
}
Example.defaultProps = {
fName: "Hello",
lName: 'world'
}
Run Code Online (Sandbox Code Playgroud)
希望这对你有帮助。如有疑问,请放心。
| 归档时间: |
|
| 查看次数: |
447 次 |
| 最近记录: |