javascript - 如何在 React Native 中设置 props 的默认值

标签 javascript react-native jsx react-props

我需要知道如何为 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.introduction0 我想将其设置为空,例如:“”,或者具有其他值,例如:“未设置”。

谢谢

最佳答案

您可以使用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/

相关文章:

javascript - 解析云代码助手功能不起作用

Firebase 邀请 React Native

javascript - 将函数作为 props 传递时出现 ReactJS 错误

react-native - 错误: Unable to resolve module `react-native-gesture-handler`

react-native - 如何在 React Native 渲染之前获取文本大小或 View 大小

javascript - 我如何包装在 .map 方法中重复的 div

javascript - 哪个更好 - 在传递参数时解构 props 或在函数内部解构 props?

javascript - 正则表达式匹配附加可选单词

javascript - 通过solidity向Aave提供ETH

javascript - 如何以百分比形式获取 Chrome 扩展程序的 CPU 使用率