[英]Why my useState is not updating the value?
I have an useState that gets the value from DB:我有一个从数据库获取值的 useState:
const [checklist, setChecklist] = useState(undefined);
And I have this condition, that when happens, I would like to change the useState:我有这个条件,当发生时,我想改变 useState:
let newItems = [];
let newChecklist = {};
if (project?.analysisData?.actionPlan == "no") {
newItems = checklist?.items?.filter(
(item) => item.conformityStatus !== "initial_status"
);
newChecklist = { ...checklist, items: newItems };
}
Notice that the items were filtered.请注意,这些项目已被过滤。
And I'm trying to change the value from my useState with useEffect:我正在尝试使用 useEffect 更改 useState 的值:
useEffect(() => {
setChecklist(newChecklist);
}, []);
But for some reason it's not changing.. Anyone can help me?但由于某种原因它没有改变..任何人都可以帮助我吗? Thank you so much!
太感谢了!
I created a new useState:我创建了一个新的 useState:
const [changeChecklist, setChangeChecklist] = useState(true);
called a setTimeout, to force some "change" after the component Mount:称为 setTimeout,以在组件安装后强制进行一些“更改”:
setTimeout(() => {
setChangeChecklist(false);
}, 550);
and called the useEffect with the change of my new useState:并通过更改我的新 useState 来调用 useEffect:
let newItems = [];
useEffect(() => {
if (project?.analysisData?.actionPlan == "no") {
newItems = checklist?.items?.filter(
(item) => item.conformityStatus !== "initial_status"
);
setChecklist({ ...checklist, items: newItems });
}
}, [changeChecklist]);
Maybe its not the best solution, but it was the one that I figured out to solve my problem:)也许它不是最好的解决方案,但它是我想出的解决问题的方法:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.