我的还原器集合存储在rootReducer中(有普通还原器和切片还原器(。
我需要使用useSelector钩子访问这些减速器内部的状态。
我的商店:
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);
}
}
});
另一个样品还原剂(正常(:
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 reducers = {
getName: setNameList.reducer,
getSomething: somethingReducer.reducer
}
不要忘记导入减速器,并以相同的方式使用其他减速器。