I'am currently new on react js and redux and I start studying first the Login Form. Right now I experience problem when I press the button sign in, the state save on redux action however when I refresh the browser the redux action removed meaning all the state remove automatically when the browser refresh. So my question here how to save the redux action permanent even If i refresh the browser it will not remove?
I read some thread here in stackoverflow, they suggest to use the redux-persist and this library is new for me. I don't really understand where should I put the persist library on my file.
I will share my sample work that I already made:
Store.js
import { configureStore } from '@reduxjs/toolkit';
import userReducer from '../features/userSlice';
export default configureStore({
reducer: {
user: userReducer,
}
});
UserSlice.js
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;
export default userSlice.reducer;
Root Index.js
import store from './app/store';
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);
Here is the sample output Redux Action after I press the submit button:
Output when I refresh the browser:
Error:
Hope it helps.