React.js - default prop is not used with `null` is passed

前端 未结 3 628
太阳男子
太阳男子 2020-12-30 19:47

I have default props in my React component:

PropertyTitleLabel.defaultProps = {
    bedrooms: 1,
    propertyType: \'flat\'
};
PropertyTitleLabel.propTypes =         


        
3条回答
  •  独厮守ぢ
    2020-12-30 20:30

    I think there's a distinction between null and undefined that is made when dealing with the defaultProps. The null value could be intended behavior and thus isn't replaced by your defaults, while undefined is not and will be replaced.

    As stated in the docs

    [...] used to ensure that this.props.value will have a value if it was not specified by the parent component.

    Here is a related issue.

提交回复
热议问题