Updating state with props on React child component

后端 未结 4 814
一向
一向 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 01:52

    It would also be good to check if you even need to update the state, since this will cause a re-render.

    componentWillReceiveProps(newProps) {
      if (this.state.name !== newProps.name) {
        this.setState({name: newProps.name});
      }
    }
    

提交回复
热议问题