[英]React rerender component using hooks when property in an array of object changes
[英]Error when using setState to set an empty array to object property ( react Hooks)
我对这条线有问题:
useEffect(() => {
if (AplicationsList) {
setDetail({ ...Detail, components: AplicationsList });
}
}, [AplicationsList]);
当 AplicationsList 它是一个空数组 [] 时,它会给我一个错误,例如 Detail 是未定义的。 但是当它不为空时工作正常......无论数组是否为空,我都必须在 Detail 的 components 属性上设置值。 知道为什么会这样吗?
这是我在依赖作为道具传递的“详细信息”的子组件中面临的错误:无法读取未定义的属性 map
我收到了这个反应警告: can't perform a react state update in an unmounted component
我已经尝试过这个功能更新,但仍然给我同样的错误: setDetail(Detail => ({...Detail, components: AplicationsList, }));
根据您的问题,我了解Details
是AplicationsList
而 AplicationsList 是一个数组。
我认为您已经以这种方式设置细节的 state 或类似于此const [Details, setDetails] = useState();
. 问题在于 Details 的初始值未定义,而您正试图解构未定义。 而是将其设置为const [Details, setDetails] = useState({});
在AplicationsList
中,您正在检查 AplicationsList 是否为空,但if(AplicationsList)
即使对于空数组也会返回 true。 因此,请检查长度, if(AplicationsList.length > 0)
建议:最好让 state 变量名称为小写,并按照 camleCase 格式命名变量。 而不是Details
和ApplicationsList
,最好重命名为details
和applicationsList
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.