react-redux useSelector 告诉“Uncaught TypeError: Object(...) is not a function"; [英] react-redux useSelector is telling "Uncaught TypeError: Object(...) is not a function"
问题描述
我对 React 钩子很陌生,我正在尝试用 useSelector
、 替换
.这就是我所拥有的:connect
、mapStateToProps
等使用调度
I am quite new in React hooks, I am trying to replace the connect
, mapStateToProps
and so forth with useSelector
, useDispatch
. This is what I have:
import React from 'react';
import { useSelector } from 'react-redux';
function MyComponent(props) {
const data = useSelector(state => state.app.loadingData);
return (
<div>
{data}
</div>
);
}
);}
根据
所以,app
存在于 redux 状态树中,loadingData
也存在.无论如何,我的浏览器控制台指向 const data = useSelector(state => state.app.loadingData);
大喊我有这个错误:
So, app
exists in the redux state tree and loadingData
exists as well. Regardless of that, my browsers console points to const data = useSelector(state => state.app.loadingData);
yelling me that there is this error:
未捕获的类型错误:Object(...) 不是函数
Uncaught TypeError: Object(...) is not a function
那么,我做错了什么或者我在这里遗漏了什么?
So, what am I doing wrong or what am I missing here?
推荐答案
解决方案:
也许不是最好的方法,也不是最漂亮的方法,但对我来说是可行的解决方案:
Maybe not the best way, or the prettiest way, but working solution for me:
const data = useSelector(state => state.getIn(['app', 'loadingData']);
希望这会有所帮助.拜托,如果可以改进此解决方案,redux 团队可能会在这里提供帮助.
Hope this could be helpful. Please, the redux team might be providing light here if this solution could be improved.
这篇关于react-redux useSelector 告诉“Uncaught TypeError: Object(...) is not a function";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!