React… setting child Properties from State

Question!

I have read that it is an anti-pattern to set state from props, but what about setting child properties from state? Is this code okay, or is there a better practice.

Let's say we have an APP component that holds and changes it's state. Is it good practice to pass that state down to child display components as props?

   var APP = React.createClass({

   getInitialState() {
       return {
           items: [1,2,3],
           title: 'Simple Sample'
       };
   },

   render() {
       return <div>
           <Display title={this.state.title} />
           <List items={this.state.items} />
       </div>
   }
});

What about something like this:

   var APP = React.createClass({

   getInitialState() {
       return {
           items: [1,2,3],
           title: 'Simple Sample'
       };
   },

   render() {
       return <div>
           <DisplayList {...this.state} />
       </div>
   }
});


Answers

Passing a parent's state into a child's props is the primary way of communicating with children. Passing function references to a child's props is the primary way of allowing the child to communicate back to the parent.

I personally avoid the



This video can help you solving your question :)
By: admin