React PropTypes defaultProps doesnt work, when "null" is passed

Viewed 297
2 Answers

The default props is used only when you not marking props as required

Here is also an eslint rule for that

When you mark it as required, you are sure that the props will be passed, so there will be no reason to have defaultProps

MyComponent.propTypes = {
  firstName: PropTypes.string,
};

MyComponent.defaultProps = {
  firstName: "John Doe"
};

OR

MyComponent.propTypes = {
  firstName: PropTypes.string.isRequired,
};

Also, when you are passing props as null, meaning that the prop's variable is not referenced to any actual value, which is equivalent to not passing it.

Related