简体   繁体   English

reactjs - 为什么通过父道具和组件onChange更新表单值状态?

[英]reactjs - Why have form value state updated through both parent props and component onChange?

I'm reading the Fullstack React book, and in their example on form validation, they create their own Field component (pg. 204 - 212), and then store the field value in both the Field state and parent state, which is confusing to me. 我正在阅读Fullstack React书,在他们的表单验证示例中,他们创建了自己的Field组件(第204-212页),然后将字段值存储在Field状态和父状态中,这让人感到困惑。我。 Their Field component has a value prop as well as a state containing value . 它们的Field组件具有value prop和包含value的状态。 The parent component needs to know about each field value, so that it can do form validation as a whole, and so it also has a state containing value . 父组件需要知道每个字段值,以便它可以作为整体进行表单验证,因此它还具有包含value的状态。

Within Field, they handle value changes by both setting the Field state when the input value changes, and by using getDerivedStateFromProps when the value prop changes: 在现场,他们处理value由双方设置现场状态输入时改变value变化,并通过使用getDerivedStateFromProps的当value道具的变化:

//(within Field)
getDerivedStateFromProps(nextProps) {
     return {value: nextProps.value}
}

onChange = evt => {
    const name = this.props.name;
    const value = evt.target.value;
    const error = this.props.validate ? this.props.validate(value) : false;

    this.setState({value, error});

    this.props.onChange({name, value, error});
};

They also sync the value state in the other direction to the parent by calling the parent's onInputChange function (passed as the onChange prop): 它们还通过调用父的onInputChange函数(作为onChange prop传递)将另一个方向的值状态同步到父级:

//(within parent component)
onInputChange = ({name, value, error}) => {
    const fields = Object.assign({}, this.state.fields);
    const fieldErrors = Object.assign({}, this.state.fieldErrors);

    fields[name] = value;
    fieldErrors[name] = error;

    this.setState({fields, fieldErrors});
};

The book doesn't really explain why they duplicate the state like this, except to say, 这本书并没有真正解释为什么他们像这样复制国家,除了说,

"There are only two pieces of data that Field will need, the current value and error. Like in previous sections where our form component needed that data for its render() method, so too does our Field component." “Field只需要两个数据,当前值和错误。就像我们的表单组件需要其render()方法的数据的前几节一样,我们的Field组件也是如此。”

and also 并且

"One key difference is that our Field has a parent, and sometimes this parent will want to update the value prop of our Field. To allow this, we'll need to create a new lifecycle method, getDerivedStateFromProps() to accept the new value and update the state." “一个关键的区别是我们的Field有一个父级,有时这个父级会想要更新我们Field的值prop。为此,我们需要创建一个新的生命周期方法,getDerivedStateFromProps()来接受新值并更新州。“

I'm just a beginner, but in my mind, it would make more sense to ditch the value state altogether within Field, and have it just passed in as a prop. 我只是一个初学者,但在我看来,在Field中完全抛弃value状态更有意义,并将它作为道具传递出来。 When the input changes, call the onChange method with Field, and call parent's onInputChange within that. 当输入改变时,用Field调用onChange方法,并在其中调用parent的onInputChange。 Have onInputChange update the parent state about the field's value, and pass down the field value as a prop to the field. 让onInputChange更新关于字段值的父状态,并将字段值作为prop传递给字段。 The way it's done now seems sort of redundant and more error prone. 它现在的方式似乎有点冗余,更容易出错。 Any insight as to why they do it this way? 有关他们为何这样做的任何见解?

Haven't read the book, but here I will explain why I would write such a code. 没有读过这本书,但在这里我将解释为什么我会编写这样的代码。

The main point in having the two states is to make the Field component more generic. 拥有这两种状态的要点是使Field组件更通用。

In that specific case, the parent happens to also save the value in his state, and the Field component becomes a controlled component by updating his state from the received props on getDerivedStateFromProps . 在该特定情况下,父节点也恰好将值保存在其状态中,并且Field组件通过在getDerivedStateFromProps上从接收到的props更新其状态而成为受控组件。

However there is still the possibility to use the Field component as an uncontrolled component, then the Field 's state would be the only source of truth. 然而,仍然有可能将Field组件用作不受控制的组件,那么Field的状态将是唯一的事实来源。

In both cases there's only a single source of truth, which maintains React's way of doing things, however the Field component can be used in both a controlled and uncontrolled form. 在这两种情况下,只有一个单一的事实来源,它保持了React的做事方式,但是Field组件可以以受控和不受控制的形式使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React.js,父组件,状态和道具 - Reactjs, parent component, state and props ReactJs子组件未通过道具更新 - ReactJs Child Component is not getting updated through Props 在子组件中记录道具会给出更新的值,而在父组件中定义的 state 本身仍未更新 - Logging the Props in child component gives the updated value whereas state itself defined in the parent still not gets updated ReactJS 表单:父组件状态更新,但子字段被清空,道具保持不变 - ReactJS Form: Parent Component state updates, but Child's field gets emptied & props remain unchanged ReactJS更新状态未实时传递给子组件 - ReactJS Updated state not passed in realtime as props to child component 父组件的 state 更新后,子组件中的道具未更新 - props not updating in child component after parent component's state is updated 如果在 reactjs 中的这些子组件之一中更新,为什么两个子组件的角色基础父级具有不同的状态 - why role base parent of two child component have different state if update in one of these child component in reactjs 为什么当状态在ReactJs上有对象时我的onChange不起作用 - Why my onChange do not work when state have object on ReactJs 道具值没有在vue中将更新的值从父组件发送到子组件 - props value not sending the updated value from parent to child component in vue 通过道具将父组件的当前状态传递给子组件 - Pass the current state of parent component to children through props
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM