【发布时间】:2021-11-17 07:23:46
【问题描述】:
App.js:
function App() {
PreProcess();
const user = useSelector(selectUser); // user initialized to null
return user ?
(
<p>{user}</p>
)
:
(<p>Authenticating and Authorizing</p>)
}
export default App;
preProcess.js:
export const PreProcess = async () => {
await readEnvVars(); // read env vars in local json file
Auth(); // need environment variables for authentication and authorization
}
auth.js
export const Auth = async () => {
const dispatch = useDispatch();
// some logic for authentication and authorization to get the user
dispatch(setUser(userId));
}
收到错误
未捕获(承诺中)错误:无效的挂钩调用。钩子只能是 在函数组件的主体内部调用。
在 auth.js:
const dispatch = useDispatch();
为什么会出现此错误以及如何解决?
【问题讨论】:
标签: javascript reactjs async-await react-hooks redux-toolkit