【发布时间】:2020-10-04 09:26:16
【问题描述】:
在App 中,我有一个子组件,它有一个包含<input> 的子组件和另一个包含<input> 的子组件。结构如下:
<App>
<Parent>
<ClassWithInput1 />
</Parent>
<ClassWithInput2 />
</App>
在App.state 中,我有一个字符串val,可以通过任一<input> 字段设置,并在更新时在两个<input> 字段中使用defaultValue 属性显示。
使用ClassWithInput1 更新时,ClassWithInput2 中的<input> 的值会正确更新。但是,当我在ClassWithInput2 中更新它时,更改不会反映在ClassWithInput1 中。 App.state.val 在这两种情况下都会正确更新。我检查了 Chrome 检查器,HTML 中的value 属性在ClassWithInput1 中是正确的,但实际更改并没有显示在页面上。
以下是该问题的沙盒示例:https://codesandbox.io/s/zen-thunder-z1p81?file=/src/App.js。
我该如何解决这个问题?
【问题讨论】:
标签: javascript html reactjs react-component