Updating state with props on React child component

后端 未结 4 819
一向
一向 2021-02-01 01:12

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 t

4条回答
  •  死守一世寂寞
    2021-02-01 02:07

    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.

提交回复
热议问题