简体   繁体   English

React:如何使用 getDerivedStateFromProps() 设置组件状态。 它干扰使用相同状态变量的受控组件

[英]React: How to use getDerivedStateFromProps() to set component state. It interferes with controlled component that uses the same state variable

I have a component that receives a variable as a prop.我有一个接收变量作为道具的组件。 The getDerivedStateFromProps() is typical, meaning that when nextProps.someProp doesn't equal prevState.someStateVar, that prop is assigned to state variable. getDerivedStateFromProps() 是典型的,这意味着当 nextProps.someProp 不等于 prevState.someStateVar 时,该道具被分配给状态变量。

I also have a Controlled element - an HTML text Input element which is associated with the same state variable.我还有一个 Controlled 元素 - 一个 HTML 文本输入元素,它与相同的状态变量相关联。

The problem is, when I change the state variable through the controlled element, getDerivedStateFromProps() executes and sets the value back to the prop received earlier.问题是,当我通过受控元素更改状态变量时, getDerivedStateFromProps() 执行并将值设置回之前收到的道具。

As a React novice, I don't understand why this happens.作为 React 新手,我不明白为什么会发生这种情况。 The above action should only occur when a new prop value is received, like the name 'nextProps' suggests.上面的动作应该只在接收到一个新的 props 值时发生,就像名称“nextProps”所暗示的那样。

Please suggest how to get the desired behaviour:请建议如何获得所需的行为:

  1. Use prop to set initial state使用 prop 设置初始状态
  2. Let my controlled element (html input tag) set next values of the state variable让我的受控元素(html 输入标签)设置状态变量的下一个值
  3. iff new prop value is received, assign that to the next value of the state variable如果接收到新的 prop 值,则将其分配给状态变量的下一个值

The article - "You Probably Don't Need Derived State" was true.文章 - “你可能不需要派生状态”是真的。

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

I referred the controlled component email example.我提到了受控组件电子邮件示例。

Now, the data of all the child components is stored in the parent's state.现在,所有子组件的数据都存储在父组件的状态中。 The parent's state is passed down as a prop to the child components.父组件的状态作为道具传递给子组件。 A method in the parent component edits the state variables.父组件中的方法编辑状态变量。 This method is passed down to the child components as a prop.此方法作为道具传递给子组件。

Hence, the single source of truth is established and it is stored in the parent component making its children fully controlled components.因此,建立了单一事实来源并将其存储在父组件中,使其子组件完全受控。 This is how I did it.我就是这样做的。

暂无
暂无

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

相关问题 我在 React 组件上进行了一个 API 调用,从中获取数据并将其传递给一个状态。 我也想在另一个组件上使用相同的数据 - I made an API call fetching data from it on a React component and passed it on to a state. I want to use the same data on another component too React 16.X getderivedstatefromprops返回新状态。是否可以在组件内部再次更改相同状态? - React 16.X getderivedstatefromprops returned new state .Is possible to change that same state inside component again? React:在 useEffect Hook 中执行函数以更新状态。 使用状态值返回组件 A 或 B - React: Execute function in useEffect Hook to update state. Use the state value to return component A or B 如何将 redux state 变量设置为组件 state - How to set redux state variable to component state 使用 getDerivedStateFromProps 从父级反应更新子组件 state - React update child component state from parent using getDerivedStateFromProps 将 State 设置为 getDerivedStateFromProps (设置 state,反应) - Set State into getDerivedStateFromProps (set state, React) 响应 dnd 将放置的项目的对象推送到组件的状态。 - React dnd to push object of dropped item to component's state. React Function 组件使用变量(不是状态) - React Function Component use variable (not state) 如何用使用效果替换组件更新并在其中设置 state 以做出反应 - How to replace Component Did update with use Effect and set state in it in react 如何设置组件上的状态? - How to set a state that is on a component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM