I will just note that I understand the standard data flow of Redux, which you have an action creator which notify the type to the reducer and the reducer will do the magic of updating the state.
yet in React-Redux (redux-toolkit) when I dispatch something it will be a reducer directly, thus conceptually the action creator is missing when I think about it and I'm struggling to understand how the 'reducer' determine the type of action.
in the example below I'm dispatching setRegistrationData which is a reducer not an action creator.
Some code examples to see if my code is incorrect rather then my understanding:
Dispatching the reducer:
dispatch(setRegistrationData())
Store:
const store : Store = configureStore({
reducer: {
modalWindow: modalWindowReducer,
yPosition : windowYPositionSlice,
loginData: loginDataSlice,
formValidation : loginFormValidationSlice,
registrationData : registrationDataSlice,
}
})
// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>;
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState}
export type AppDispatch = typeof store.dispatch;
export default store;
A random Slice
// interface RegistrationData
interface RegistrationData{
userName : string;
email : string;
password : string;
confirmPassword : string;
}
// Need to change it to an object
const initialState : RegistrationData = {
userName : '',
email : '',
password : '',
confirmPassword : ''
};
export const registrationData = createSlice({
name: 'registrationData',
initialState,
reducers: {
setRegistrationData(state){
Object.assign(state, { userName : 'DData', email : 'data@data.com', password : 'dddd', confirmPassword : 'ddd'});
}
}
})
export const { setRegistrationData } = registrationData.actions;
export default registrationData.reducer;