Updating state on props change in React Form

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

    I think use ref is safe for me, dont need care about some method above.

    class Company extends XComponent {
        constructor(props) {
            super(props);
            this.data = {};
        }
        fetchData(data) {
            this.resetState(data);
        }
        render() {
            return (
                <Input ref={c => this.data['name'] = c} type="text" className="form-control" />
            );
        }
    }
    class XComponent extends Component {
        resetState(obj) {
            for (var property in obj) {
                if (obj.hasOwnProperty(property) && typeof this.data[property] !== 'undefined') {
                    if ( obj[property] !== this.data[property].state.value )
                        this.data[property].setState({value: obj[property]});
                    else continue;
                }
                continue;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-02 05:18

    There is also componentDidUpdate available.

    Function signatur:

    componentDidUpdate(prevProps, prevState, snapshot)
    

    Use this as an opportunity to operate on the DOM when the component has been updated. Doesn't get called on initial render.

    See You Probably Don't Need Derived State Article, which describes Anti-Pattern for both componentDidUpdate and getDerivedStateFromProps. I found it very useful.

    0 讨论(0)
  • 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
    0 讨论(0)
  • 2020-12-02 05:19

    Use Memoize

    The op's derivation of state is a direct manipulation of props, with no true derivation needed. In other words, if you have a prop which can be utilized or transformed directly there is no need to store the prop on state.

    Given that the state value of start_time is simply the prop start_time.format("HH:mm"), the information contained in the prop is already in itself sufficient for updating the component.

    However if you did want to only call format on a prop change, the correct way to do this per latest documentation would be via Memoize: https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html#what-about-memoization

    0 讨论(0)
  • 2020-12-02 05:20

    Apparently things are changing.... getDerivedStateFromProps() is now the preferred function.

    class Component extends React.Component {
      static getDerivedStateFromProps(props, current_state) {
        if (current_state.value !== props.value) {
          return {
            value: props.value,
            computed_prop: heavy_computation(props.value)
          }
        }
        return null
      }
    }

    (above code by danburzo @ github )

    0 讨论(0)
提交回复
热议问题