我的 reducer 集合存储在 rootReducer 中(有普通的 reducer 和 slice reducer)。
我需要使用 useSelector 钩子访问这些 reducer 中的状态。
我的商店:
const store = configureStore({reducer : rootReducer});
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'.
答案 0 :(得分:2)
您的 RootReducer 有问题。按照以下方式更改 const 变量:
const reducers = {
getName: setNameList.reducer,
getSomething: somethingReducer.reducer
}
不要忘记导入reducer并以同样的方式使用其他reducer。