[英]order of execution in useEffect clean up
useEffect(() => {
return history.replace({
pathname: "path/A",
});
}, []);
useEffect(() => {
return history.replace({
pathname: "path/B",
});
}, []);
When the component unmounts, what will be the URL?卸载组件后,URL 将是什么? What determines the order of execution of the above 2 useEffect
?是什么决定了上述 2 useEffect
的执行顺序?
Due to the nature of you passing no deps
;由于您不通过任何部门的deps
; both of these useEffect
s will only be rendered on component mount.这两个useEffect
都只会在组件安装时呈现。 Therefore, the result is (and always will be) "path/B"
.因此,结果是(并且永远是) "path/B"
。
If you wanted it to change on component unmount, you'd need to add a return
function to one of them.如果您希望它在组件卸载时更改,您需要将return
function 添加到其中之一。
useEffect(() => {
history.replace({ pathname: "path/B" });
return () => history.replace({ pathname: "path/B" });
}, []);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.