Updating state on props change in React Form

前端 未结 11 1974
时光说笑
时光说笑 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 (
                 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;
            }
        }
    }
    

提交回复
热议问题