I am a beginner and I do not know if there is a difference between createSlice and createReducer.
Which shoudl I choose ?.
Second of all, I watched a tutorial today. The guy just made a store.js file and a userslice.js file. Very Simple.
But he made it a bit different from the redux documentation. He exported three CONST at the end of the userSlice.js instead of two like in the documention. (I have made a comment) If I change state.user.user to state.user.wtf...... I will run in a error after a while, because some components do not have acess to the user object anymore.
userSlice
import { createSlice } from '@reduxjs/toolkit';
export const userSlice = createSlice({
name: "user",
initialState: {
user: null,
},
reducers: {
login: (state, action) => {
state.user = action.payload;
},
logout: (state) => {
state.user = null;
},
},
});
export const { login, logout } = userSlice.actions;
export const selectUser = (state) => state.user.user; // What is that ? and why is it state.user and then a third user Object ???
export default userSlice.reducer;
in app. js he made that command
const user = useSelector(selectUser);
instead
const user = useSelector(state => state.user.user);
I really do nor understand the difference
store.js file
import { configureStore } from '@reduxjs/toolkit';
import userReducer from '../features/userSlice'
export default configureStore({
reducer: {
user: userReducer,
},
});
could somebody please explaine me that. I am so confused