Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass parent state to its child components?

Tags:

I am new in React ES6 and I think I am modifying the state in a wrong way. My code is like this when I set state on parent component:

class App extends React.Component  {
constuctor(props)  {
     super(props);
     this.state = {name:"helloworld"};
}
 render() { 
  return( 
   <ChildComponent parentObj={this} /> // parentObj for parent context as props on child components  
  ); 
 } 
}

My problem is in other child components, I have to do it repeatitively, is there another way of doing it? I have no problem with React.createClass, but I want to code in es6 so i have this problem.

like image 358
gpbaculio Avatar asked Jan 16 '17 15:01

gpbaculio


People also ask

Is it possible to change parent component state from the child component?

To update the parent state from the children component, either we can use additional dependencies like Redux or we can use this simple method of passing the state of the parent to the children and handling it accordingly.

How do you access the state in the parent component from a child component?

You may access the child state by passing a callback to the child component. Now if you click the button in the child component, you will execute the function passed from the parent and have access to the child component's state variables.


1 Answers

If you wanna pass the state {name:"helloworld"} do it like that:

class App extends React.Component {
constuctor(props)  {
     super(props);
     this.state = {name:"helloworld"};
}
 render() { 
  return( 
   <ChildComponent {...this.state} /> 
  ); 
 } 
}

and in the child component you can do:

<Text>{this.props.name}</Text>

But If you want to pass the props of the component to it's child:

   class App extends React.Component {
    constuctor(props)  {
         super(props);
         this.state = {name:"helloworld"};
    }
     render() { 
      return( 
       <ChildComponent {...this.props} />
      ); 
     } 
    }

and in the child component you can do:

<Text>{this.props.stuff}</Text>//place stuff by any property name in props

Now if you want to update the state of parent component from the child component you will need to pass a function to the child component:

 class App extends React.Component {
    constuctor(props)  {
         super(props);
         this.state = {name:"helloworld"};
    }
    update(name){
       this.setState({name:name})// or with es6 this.setState({name})
    }
    render() { 
     return( 
      <ChildComponent {...this.props, update: this.update.bind(this)} />
     ); 
    } 
  }

and then in child component you can use this : this.props.update('new name')

UPDATE

use more es6 and removed constructor

class App extends React.Component {
    state = {name:"helloworld"};

    // es6 function, will be bind with adding .bind(this)
    update = name => {
       this.setState({name:name})// or with es6 this.setState({name})
    }

    render() { 
     // notice that we removed .bind(this) from the update
     return(
     //send multiple methods using ','
      <ChildComponent {...this.props, update = this.update} />
     ); 
    } 
  }
like image 113
challenger Avatar answered Oct 07 '22 13:10

challenger