I started an infinite loop with [watchEffect] #7132
JessYan0913
started this conversation in
General Discussions
Replies: 2 comments 4 replies
-
You can use the import { ref, reactive, watchEffect } from "vue";
console.log("start");
const num = ref(0);
const state = reactive({
num: 0,
});
const setNum = async (num) => {
state.num = await Promise.resolve(() => num.value);
+ debugger;
};
watchEffect(() => {
console.log("efft");
setNum(num);
+ debugger;
console.log(state.num);
});
setTimeout(() => {
num.value = 1;
}); |
Beta Was this translation helpful? Give feedback.
0 replies
-
It's quite straighforward:
in short: don't read from and write to the same reactive state in |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Reactive variable modification happens when I call async method inside watchEffect and async method.
At the same time, using reactive variables in watchEffect will lead to an infinite loop of watchEffect.
code show as below:
infinite loop deom: https://codesandbox.io/s/patient-dream-zmhqse?file=/src/App.vue
not infinite loop deom: https://codesandbox.io/s/delicate-bush-zwqhht?file=/src/App.vue
How can this phenomenon be explained?
Beta Was this translation helpful? Give feedback.
All reactions