reactjs - react defaultProps null 与 undefined

标签 reactjs react-proptypes

假设我们有一个名为 Button 的组件和一个名为 classN 的属性,这不是必需的。

Button.propTypes = {
  classN: PropTypes.string,
};

将该属性的 defaultProps 定义为 null 或定义为 undefined 之间有什么区别?

Button.defaultProps = {
  classN: undefined,
}

对比

Button.defaultProps = {
  classN: null,
}

最佳答案

null 值设置为 props 可能是有意为之,并用于指示生命周期逻辑。添加到@hemant-kumar 的评论中,默认属性默认为 undefined,可用于指示尚未设置但将要设置的值。

参见 this question

关于reactjs - react defaultProps null 与 undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60614483/

相关文章:

reactjs - 合并在 navigation.navigate (React Native) 中做什么?

reactjs - 无法导入 react-addons-shallow-compare,Typescript

reactjs - Expo React Native 无法在 Android : "The system cannot find the path specified" 上启动项目

javascript - React,何时使用 propTypes

javascript - React JS 'propTypes' 验证器运行两次

javascript - 如何在我的组件中导入 javascript min 文件?

android - Expo/React Native - 添加可点击图标以及 StackNavigator 对象

javascript - 流类型注释是否包含在运行时的生产代码中?

javascript - undefined 不是对象(评估 '_react.PropTypes.object' )

javascript - 如何在 React 中验证 Prop 的类及其字段(嵌套对象)的类?