Updating state on props change in React Form

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

    From react documentation : https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html

    Erasing state when props change is an Anti Pattern

    Since React 16, componentWillReceiveProps is deprecated. From react documentation, the recommended approach in this case is use

    1. Fully controlled component: the ParentComponent of the ModalBody will own the start_time state. This is not my prefer approach in this case since i think the modal should own this state.
    2. Fully uncontrolled component with a key: this is my prefer approach. An example from react documentation : https://codesandbox.io/s/6v1znlxyxn . You would fully own the start_time state from your ModalBody and use getInitialState just like you have already done. To reset the start_time state, you simply change the key from the ParentComponent

提交回复
热议问题