So I have a redux store in my react-project. The variables in the redux state like the user are subscribed to localStorage items to prevent resetting of values.
My store:
const pixStore = createStore(
combinedReducers, /* preloadedState, */
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);
pixStore.subscribe(() => {
localStorage.setItem('token', pixStore.getState().token);
localStorage.setItem('user', pixStore.getState().user);
});
reducers:
import {combineReducers} from 'redux';
import {UserReducer, tokenReducer} from './authReducers';
// combine all reducers here
const combinedReducers = combineReducers({
user: UserReducer,
token: tokenReducer,
})
export default combinedReducers;
So when an action is dispatched, I want to set some values of the redux state to null. this is how am doing it now.
const tokenReducer = (state=localStorage.getItem('token'), action) => {
switch(action.type){
default:
return state;
case "REFRESH_TOKEN":
/* Set token if not exists or update the existing token */
return state = action.payload;
case "CLEAR_TOKEN":
return state = null;
}
}
when I do this, the localStorage item's value is set to null
but the state variable's value becomes "null"
like it's a string. Because of this my functions don't detect the state variable to be null and it causes a lot of issues.
Why is this happening? Anyway I can fix it? please help me, thanks!!
