【发布时间】:2021-08-25 11:51:09
【问题描述】:
我对这条线有问题:
useEffect(() => {
if (AplicationsList) {
setDetail({ ...Detail, components: AplicationsList });
}
}, [AplicationsList]);
当 AplicationsList 它是一个空数组 [] 时,它会给我一个错误,例如 Detail 未定义。但是当它不为空时工作正常...... 无论数组是否为空,我都必须在 Detail 的 components 属性上设置值。 知道为什么会这样吗?
这是我在子组件中面临的错误,它依赖于作为道具传递的“详细信息”:cannot read property map of undefined
我收到了这个反应警告: can't perform a react state update in an unmounted component
我已经尝试过这个功能更新,但仍然给我同样的错误: 设置详细信息(详细信息 =>({ ...细节, 组件:应用程序列表, }));
【问题讨论】:
-
如果新状态是使用前一个状态计算的,React recommends using a functional update(您将函数传递给 setter)。您是否尝试过使用此变体?
setDetail((detail) => ({ ...detail, components: applicationsList }))您能否将您面临的确切错误(包括堆栈跟踪)添加到问题中? -
脱离@AbhishekGowda28 所说的,检查条件中数组的长度。我认为分配给空数组的变量评估为真。
标签: javascript arrays ecmascript-6 react-hooks destructuring