Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating state with props on React child component

I have a React app, where props from a parent component are passed to a child component and the props then set the state on the child.

After I send an updated value to the parent component, the child component isn't updating the state with the updated props.

How do I get it to update the state on the child component?

My pared-down code:

class Parent extends React.Component {     constructor (props) {         super(props);         this.state = {name: ''}      }     componentDidMount () {         this.setState({name: this.props.data.name});     }     handleUpdate (updatedName) {         this.setState({name: updatedName});     }     render () {         return <Child name={this.state.name} onUpdate={this.handleUpdate.bind(this)} />     } }   class Child extends React.Component {     constructor (props) {         super(props);         this.state = {name: ''}      }     componentDidMount () {         this.setState({name: this.props.name});     }     handleChange (e) {         this.setState({[e.target.name]: e.target.value});     }     handleUpdate () {         // ajax call that updates database with updated name and then on success calls onUpdate(updatedName)     }     render () {         console.log(this.props.name); // after update, this logs the updated name         console.log(this.state.name); // after update, this logs the initial name until I refresh the brower         return <div>                         {this.state.name}                     <input type="text" name="name" value={this.state.name} onChange={this.handleChange} />                     <input type="button" value="Update Name" onClick={this.handleUpdate.bind(this)} />                 </div>     } } 
like image 588
Adam White Avatar asked Aug 25 '16 21:08

Adam White


People also ask

Can I change state from child component React?

React hooks are introduced in React 16.8. If you are familiar with the class components then there is no difference to change the parent component state from child component. In both cases, you have to pass the callback function to the parent.

Can we change props in child component React?

@Gopika, yes you can change props value in child component as but it is not a best practise.

How do you change the state value of the child component?

To change child component's state from parent component with React, we can pass props. const Child = ({ open }) => { return <Drawer open={open} />; }; const ParentComponent = () => { const [isOpen, setIsOpen] = useState(false); const toggleChildMenu = () => { setIsOpen((prevValue) => !


2 Answers

You need to implement componentWillReceiveProps in your child:

componentWillReceiveProps(newProps) {     this.setState({name: newProps.name}); } 

Edit: componentWillReceiveProps is now deprecated and will be removed, but there are alternative suggestions in the docs link above.

like image 90
Blorgbeard Avatar answered Oct 17 '22 21:10

Blorgbeard


Calling setState() in componentWillReceiveProps doesn't cause additional re-render. Receiving props is one render and this.setState would be another render if that were executed within a method like componentDidUpdate. I would recommend doing the this.state.name !== nextProps.name in shouldComponentUpdate so it's always checked for any update.

componentWillReceiveProps(nextProps) {     this.setState({name: nextProps.name}); }  shouldComponentUpdate(nextProps) {     return this.state.name !== nextProps.name; } 
like image 37
goldbullet Avatar answered Oct 17 '22 21:10

goldbullet