Search code examples
javascriptreactjstypescriptreact-proptypes

How to declare defaultProps for the nested object?


In react component there is nested object in propTypes which works fine.

UserCard.propTypes = {
  name: PropTypes.string,
  customer: PropTypes.shape({
    email: PropTypes.string,
    phoneNumber: PropTypes.string,
  }),
};

Looking for solution to assign defaultProps for the nested objects. It seems to be current implementation is not valid solution.

UserCard.defaultProps = {
  name: 'No Name',
  email: 'No Email',
  phoneNumber: '0',
};

Solution

  • It should look like this:

    UserCard.defaultProps = {
      name: 'No name',
      customer: {
          email: 'No email',
          phoneNumber: '0',
      },
    };