Search code examples
reactjsreact-proptypes

React defaultProps null vs undefined


Let's say we have component called Button and a property called classN which is not required.

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

What are the differences between defining defaultProps for that property to null or to undefined?

Button.defaultProps = {
  classN: undefined,
}

VS

Button.defaultProps = {
  classN: null,
}

Solution

  • Settingnull values to props can be intentional and be used to dictate lifecycle logic. Adding to @hemant-kumar's comment, default props are by default undefined and can be used to indicate values that have not been set but will be.

    See this question