Home > front end >  How to declare defaultProps for the nested object?
How to declare defaultProps for the nested object?

Time:12-10

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',
};

CodePudding user response:

It should look like this:

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