reactjs - ts:如何访问redux store里面的state
问题描述
我的减速器集合存储在 rootReducer 中(具有普通减速器和切片减速器)。
我需要使用 useSelector 钩子访问这些减速器中的状态。
我的商店:
const store = configureStore({reducer : rootReducer});
根减速器:
const reducers = {
getName,
getSomething,
getCollection,
getSomethingelse
}
const rootReducer = combineReducers(reducers);
样本减速器(createSlice):
const initialState:any = {
nameList:[]
}
const setNameList = createSlice({
name:"setNameList",
initialState,
reducers:{
setNameList(state,action){
state.nameList.push(action.payload);
}
}
});
另一个 Sample Reducer(正常):
export default function somethingReducer(state = {
something:{}
}, action) {
switch(action.type){
case C.SOMETHING_ZONE:{
return {
...state
,something :action.payload
}
}
default : {}
}
return state
}
我试着做,
const name = useSelector(state => state.getName.nameList)
const something = useSelector(state => state.getSomething.something)
但出现错误:Property 'getName' does not exist on type 'DefaultRootState'.
解决方案
你的 RootReducer 有问题。按照以下方式更改 const 变量:
const reducers = {
getName: setNameList.reducer,
getSomething: somethingReducer.reducer
}
不要忘记导入减速器并以相同的方式使用其他减速器。
推荐阅读
- vue.js - 上传不同尺寸的相同图像 - Dropzonejs
- java - 无法使用 Java 读取复杂的 XML 深层嵌套结构
- android - RecyclerView 中的缩放中心项
- c# - 我在 C# 中重新定位我的对象时遇到问题
- material-ui - Material-UI - 使用嵌套弹出框悬停时保持打开的工具提示
- css - IOS中的离子搜索背景颜色
- c# - 有没有办法强制 Hangfire 每次都再次执行代码?
- angular7 - Angular 7视图初始化多次触发调用
- ios - UIBarButtonItem 突出显示选择器?
- python - 带有 matplotlib 或 seaborn xticks 的轴标签 - 行为不一致