Updating state on props change in React Form

前端 未结 11 1964
时光说笑
时光说笑 2020-12-02 04:15

I am having trouble with a React form and managing the state properly. I have a time input field in a form (in a modal). The initial value is set as a state variable in

11条回答
  •  日久生厌
    2020-12-02 05:07

    componentWillReceiveProps is being deprecated because using it "often leads to bugs and inconsistencies".

    If something changes from the outside, consider resetting the child component entirely with key.

    Providing a key prop to the child component makes sure that whenever the value of key changes from the outside, this component is re-rendered. E.g.,

    
    

    On its performance:

    While this may sound slow, the performance difference is usually insignificant. Using a key can even be faster if the components have heavy logic that runs on updates since diffing gets bypassed for that subtree.

提交回复
热议问题