I have a react admin project using React Redux.
Below is the detail:
slice.tsx:
import {createSlice} from "@reduxjs/toolkit";
const newRevisionSlice = createSlice({
name: 'newRevision',
initialState: {
dataList: [],
status: 'idle'
},
reducers: {
postSaveNewRevision: (state = { dataList:[], status:"idle" }, action) => {
return { ...state, dataList: action.payload, status: 'success' };
}
}
});
export const { postSaveNewRevision } = newRevisionSlice.actions;
export default newRevisionSlice.reducer;
component.tsx:
const newRevision = useSelector((state: RootState) => state.newRevision)
store.tsx:
declare global {
interface Window {
__REDUX_DEVTOOLS_EXTENSION_COMPOSE__?: typeof compose;
}
}
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const rootReducer = combineReducers({
fleetType: fleetTypeApi,
revisionNo: revisionNoApi,
revisionDate:revisionDateApi,
newRevision: newRevisionApi,
});
const store = createStore(rootReducer, composeEnhancers(applyMiddleware(thunk)));
const useAppDispatch = () => store.dispatch;
export type RootState = ReturnType<typeof store.getState>;
export { useAppDispatch };
ReactDOM.render(
<Provider store={store}>
<React.StrictMode>
<App />
</React.StrictMode>
</Provider>,
document.getElementById('root')
);
In react redux devtool I see that the state is updated correctly when I dispatch. However, useSelector so newRevision in the component returns undefined.
Could you please help me?
Thanks in advance.
Is there by any chance your newRevisionSlice
and the newRevisionApi
in store.jsx
are 2 different reducers? If that is the case, then you have 2 separate stores for newRevision
and they won't share states.
Similar issues that are resolved:
Additionally, because your useSelector()
return undefined
it could also be the case that there is a typo somewhere with the names of slices / reducers.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.