Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you provide default props for nested shape in React?

Is there a way in React to provide default props to a nested array of items of a certain shape?

Given the example below, my first attempt can be seen, however this doesn't work as expected.

static propTypes = {     heading: PT.string,     items: PT.arrayOf(PT.shape({         href: PT.string,         label: PT.string,     })).isRequired, };  static defaultProps = {     heading: 'this works',     items: [{         href: '/',         label: ' - this does not - ',     }], }; 

In this example, I would expect the following:

// Given these props const passedInProps = {     items: [{ href: 'foo' }, { href: 'bar' }] };  // Would resolve to: const props = {     heading: 'this works',     items: [       { href: 'foo', label: ' - this does not - ' },       { href: 'bar', label: ' - this does not - ' },     ] }; 
like image 223
Chris Avatar asked Jun 30 '16 12:06

Chris


People also ask

How do I add a default prop in React?

The defaultProps is a React component property that allows you to set default values for the props argument. If the prop property is passed, it will be changed. The defaultProps can be defined as a property on the component class itself, to set the default props for the class.

How do you access nested objects in React?

If we want to access all the values of nested objects then we have to use recursion to access each and every level of that object. And it can get more complicated according to the nesting of the object. That why we have to use recursion to get all the values and access the whole nested object.

Which object is used in component to set the type of props?

objectOf. PropTypes. objectOf ensures that the prop is an object in which all property values match the specified type.

How do you pass optional props in React?

To set optional props with default values in React TypeScript: Mark the props on the type as optional using a question mark. Provide default values for the props when destructuring them in the function's definition.


1 Answers

No. Default props are only shallowly merged.

However, one approach might be to have a Child component for each item. That way each Child component receives one object from the item array, and then the default props will be merged as you expect.

For example:

var Parent = React.createClass({    propTypes: {     heading: React.PropTypes.string,     items: React.PropTypes.arrayOf(React.PropTypes.shape({       href: React.PropTypes.string,       label: React.PropTypes.string,     })).isRequired   },    getDefaultProps: function() {     return {       heading: 'this works',       items: [{         href: '/',         label: ' - this does not - ',       }],     };   },    render: function() {     return (       <div>         {this.props.item.map(function(item) {           return <Child {...item} />         })}       </div>     );   }  });  var Child = React.createClass({    propTypes: {     href: React.PropTypes.string,     label: React.PropTypes.string   },    getDefaultProps: function() {     return {       href: '/',       label: ' - this does not - '     };   },    render: function() {     return (       <div />         <p>href: {this.props.href}</p>         <p>label: {this.props.label}       </div>     );   }  }); 
like image 52
WickyNilliams Avatar answered Sep 28 '22 10:09

WickyNilliams