Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

course4-1-code7 存疑 #200

Open
SaberA1ter opened this issue Jan 11, 2023 · 2 comments
Open

course4-1-code7 存疑 #200

SaberA1ter opened this issue Jan 11, 2023 · 2 comments

Comments

@SaberA1ter
Copy link

SaberA1ter commented Jan 11, 2023

demo: https://codesandbox.io/s/loving-platform-v1k4uh?file=/src/index.js
大部分代码与源代码一致,不同点在于我添加了67 行。
可以看到在执行完 obj.text += " and vue3"; 又执行了一次 effect。

我认为这是代码的逻辑存在一处破绽:
在 obj.ok = false; 后,obj.text 是不应该存在关联副作用的,但再触发一次 obj.text 的 get,由于上一次的 activeEffect 还存在,被判断为了这次 get 的副作用(其实根本没在 effect 中访问)

@SaberA1ter
Copy link
Author

SaberA1ter commented Jan 11, 2023

作为一个 demo,我不知道我这样会不会算太较真。
这是我修改过后的 demo:https://codesandbox.io/s/stoic-kate-ywlcyf?file=/src/index.js
对比原代码添加了 17、46 行,在 effectFn 内真实调用 fn 后将 activeEffect 置为 undefined。
这样能解决在触发 proxy get 的时候,该不该记录这个副作用函数。

@xqs121
Copy link

xqs121 commented Aug 24, 2023

《4.5 嵌套的 effect 与 effect 栈》章节中,activeEffect = effectStack[effectStack.length - 1],很巧妙的解决了这个问题。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants