useSelector使用问题
来源:10-4 仓库基础信息缓存功能的实现

蓝色西西
2021-06-01
老师,请教你两个问题:
1.如果要在打开网站时,获取cookie 中的一个name值,然后放入redux中调用,应该在哪个页面哪个事件中写代码。
2.封装了一个axios工具单元,但是在里面使用useSelector读取lang值,react版本都是17.02, 报错, 引用了react-redux,为什么axios工具单元中无法使用useSelector。老师看一下,怎么解决。
代码节选如下:
let lang = ''
if (process.browser) {
lang = useSelector((state) => state.language.locale)
}
_axios.interceptors.request.use(
(originConfig) => {
// 有 API 请求重新计时
// Vue.prototype.$_lin_jump()
if (originConfig.params && originConfig.params.showLoading) {
// showFullScreenLoading()
}
const reqConfig = { …originConfig }
reqConfig.withCredentials = false
报错信息如下:
Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
4. You might have mismatching versions of React and the renderer (such as React DOM)
5. You might be breaking the Rules of Hooks
6. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
at resolveDispatcher (/Users/Desktop/jianshu1/node_modules/react/cjs/react.development.js:1476:13
1回答
-
Jokcy
2021-06-06
你提供的信息我表示不太能看得明白,不过有一点比较明确的就是不要把hooks使用在条件判断里面
022021-06-10
全栈进阶课程 React16.8+Next.js+Koa2一步到位开发Github
651 学习 · 311 问题
相似问题