[英]React-redux useDispatch() Uncaught TypeError
I'm trying to create a simple component to dispatch an action using the React Redux hook useDispatch
- and I'm getting an error.我正在尝试创建一个简单的组件来使用 React Redux 钩子
useDispatch
来调度一个动作——但我收到了一个错误。 I've trimmed the component down to where the error occurs.我已将组件修剪到发生错误的位置。 It happens when the useDispatch function is called.
它发生在调用 useDispatch 函数时。
import { useDispatch } from 'react-redux'
const MyComp = () => {
useDispatch()
return null
}
export default MyComp
This is the error I'm seeing in dev console:这是我在开发控制台中看到的错误:
Uncaught TypeError: Object(...) is not a function
at MyComp (MyComp.js?cc4c:4)
at renderWithHooks (react-dom.development.js?d018:12938)
at mountIndeterminateComponent (react-dom.development.js?d018:15020)
at beginWork (react-dom.development.js?d018:15625)
at performUnitOfWork (react-dom.development.js?d018:19312)
at workLoop (react-dom.development.js?d018:19352)
at HTMLUnknownElement.callCallback (react-dom.development.js?d018:149)
at Object.invokeGuardedCallbackDev (react-dom.development.js?d018:199)
at invokeGuardedCallback (react-dom.development.js?d018:256)
at replayUnitOfWork (react-dom.development.js?d018:18578)
I've trimmed this down further so there are no parents in the react tree.我已经进一步削减了这个,所以反应树中没有父母。
const store = configureStore()
ReactDOM.render(
<Provider store={store}>
<MyComp />
</Provider>,
document.getElementById('root')
)
Module Versions模块版本
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-redux": "^7.1.0",
In my case, I was doing:就我而言,我正在做:
import { useDispatch } from 'react'
instead of:代替:
import { useDispatch } from 'react-redux'
🤦♂️ 🤦♂️
I got it working - I think by just stopping, building, restarting the app.我让它工作了 - 我认为只需停止、构建、重新启动应用程序。 I didn't expect
useDispatch
to need this, so I can't explain why that would have helped.我没想到
useDispatch
需要这个,所以我无法解释为什么这会有所帮助。
A note - I did try useSelector
and that did work, I could output the useSelector variable to console after import from react-redux (console.log of useDispatch was undefined).注意 - 我确实尝试过
useSelector
并且确实有效,我可以在从 react-redux 导入后将 useSelector 变量输出到控制台(useDispatch 的控制台.log 未定义)。 I could tell at that point I was on the right version of React, and this was probably a problem of my system.那时我可以判断出我使用的是正确版本的 React,这可能是我的系统的问题。
So, if you are having a similar issue.所以,如果你有类似的问题。 Try a full react app restart.
尝试完全反应应用程序重新启动。 Confirm you are loading the correct version of react.
确认您正在加载正确版本的 react。
我正在使用@reduxjs/toolkit
,并假设react-redux
已经安装。
I had a similar issue, and the fix was to update "react-redux".我有一个类似的问题,解决方法是更新“react-redux”。 Initial package.json was like this:
最初的 package.json 是这样的:
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-redux": "^6.0.1",
The problem disappeared after installing react-redux@7.2.0
安装
react-redux@7.2.0
后问题消失
upgrading from从升级
"react-redux": "6.0.1",
to到
"react-redux": "7.2.0",
fixed it for me为我修好了
run npm outdated
for an overview, and then npm update react-redux
to update redux运行
npm outdated
进行概览,然后npm update react-redux
更新 redux
我使用“@reduxjs/toolkit”:“^1.5.0”,我通过删除“react-redux”来解决这个问题:“^7.2.2”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.