反应| useState来自另一个useState,更改不会传播,如何重构?

时间:2020-01-09 08:15:08

标签: reactjs async-await state react-hooks

具有一段时间后(获取内容之后)会发生变化的状态,如果我想构造一个变量并从中建立一个新状态,则当第一个状态发生变化时,它不会传播到第二个状态

我该如何解决?我不想将两个状态合并为一个,因为这会混合执行不同功能的组件

如果有时间,您可以查看下面的代码和框

https://codesandbox.io/s/purple-sun-mx428?fontsize=14&hidenavigation=1&theme=dark

我在这里粘贴代码

DataTemplate

1 个答案:

答案 0 :(得分:1)

您可以编写一个here变量更改时运行的效果。在您的App组件中添加类似的内容。

useEffect(() => {
  // This runs when 'here' changes and then updates your other state
  setElements([{ id: "whatever", content: here }])
}, [here]) // This makes this effect dependent on the 'here' variable