我需要知道如何为 React Native 设置 props 的默认值。 我有以下代码:
<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>
如果 props.introduction
是 0
我想将其设置为空,例如:“”,或者具有其他值,例如:“未设置”。
谢谢
最佳答案
您可以使用defaultProps在 react 中
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'
}
希望这对您有帮助。如有疑问,请放心。
关于javascript - 如何在 React Native 中设置 props 的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60641822/