【发布时间】:2021-08-27 09:08:06
【问题描述】:
最近发现,如果我使用window.location.href进行重定向,而我没有指定return {},那么后面的代码会继续执行,这不是我想要的。
如果这与以下内容有关,我可以要求澄清一下:
- React.js
useEffect钩子。 -
window.location.href默认重定向后不会中断。 - 异步等待。
useEffect(() => {
const myFunction = () =>
ajax({
url: API_ROUTES.MY_EXAMPLE_ROUTE,
requestBody: {
request_message: "Hi, pls help!",
},
handleSuccess: response => {
if (condition === true)) {
window.location.href = "http://google.com";
// If I do not put return here, code will continue to execute.
return {};
}
// Code that I do not want to run after redirection
history.push(routes.error);
return {};
},
handleError: error => {
history.push(routes.error);
},
});
const init = async () => {
await myFunction();
};
init();
}, []);
【问题讨论】:
标签: javascript reactjs async-await react-hooks window.location