Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
673 views
in Technique[技术] by (71.8m points)

react 使用useEffect 异步函数 数据更新 demo不更新

image.png

使用了一个立即执行函数 异步的 但是如果我没有执行下面那个
if (code == 'update') {

        setFormData(parentObj);
}

初次渲染demo视图是不会发生变化的,对hooks的渲染机制不是很了解,求大佬指点。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

代码片段有点少,看不出具体codeparentObj的值来源(也就看问题原因),如果来自props或者state记得添加到useEffect的依赖数组里:

useEffect(() => {
  // ...
}, [code, parentOjb])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...