React defaultProps null vs undefined

Viewed 4118

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,
}
2 Answers

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

@fjplaurr I need to put an answer since I am not eligible to put a comment. By default, properties will be undefined. So it all depends, how you consume properties in your code. In case you check if a property is undefined then you will be putting a different check and in case of the null check will say if property!=null. I believe Should I set default react properties to null might help to choose the best between the two.

Related