Updating state on props change in React Form

前端 未结 11 1969
时光说笑
时光说笑 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:02

    You Probably Don't Need Derived State

    1. Set a key from the parent

    When a key changes, React will create a new component instance rather than update the current one. Keys are usually used for dynamic lists but are also useful here.

    2. Use getDerivedStateFromProps / componentWillReceiveProps

    If key doesn’t work for some reason (perhaps the component is very expensive to initialize)

    By using getDerivedStateFromProps you can reset any part of state but it seems a little buggy at this time (v16.7)!, see the link above for the usage

提交回复
热议问题