React-redux useDispatch() 未捕获的类型错误
我正在尝试创建一个简单的组件来使用 React Redux 钩子 useDispatch
来调度一个动作 - 但我收到了一个错误.我已将组件修剪到发生错误的位置.它发生在调用 useDispatch 函数时.
I'm trying to create a simple component to dispatch an action using the React Redux hook useDispatch
- and I'm getting an error. I've trimmed the component down to where the error occurs. It happens when the useDispatch function is called.
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')
)
模块版本
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-redux": "^7.1.0",
我让它工作了 - 我认为只需停止、构建、重新启动应用程序.我没想到 useDispatch
需要这个,所以我无法解释为什么会有帮助.
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.
注意 - 我确实尝试过 useSelector
并且确实有效,我可以在从 react-redux 导入后将 useSelector 变量输出到控制台(useDispatch 的控制台.log 未定义).那时我可以判断出我使用的是正确版本的 React,这可能是我的系统的问题.
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). 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.