I have a piece of state in my React & Redux App dealing with authentication. It's a boolean.
I'd like to simplify grabbing this piece of state from the Redux store but can't find examples of this.
Is this approach a bad idea? I know that mutating state is not the correct approach. I'm not sure if this constitutes a mutation though.
import {
AUTHENTICATED,
AUTHENTICATION_ERROR,
AUTHENTICATION_REMOVE,
UNAUTHENTICATED,
} from '../actions/types';
let INITIAL_STATE = false
export default function( state = INITIAL_STATE, action) {
switch(action.type) {
case AUTHENTICATED:
return !state
case UNAUTHENTICATED:
return state
case AUTHENTICATION_ERROR:
return state
case AUTHENTICATION_REMOVE:
return !state
default:
return state;
}
}
This is perfectly fine. Because you are not mutating anything from the state.
Yes, You are not mutating the
state. Because your state is just abooleanvalue and you are returning the opposite value of your state. But you are not making any change to the state.But try to use a
keyin the state. In future, if you want to add some more keys. You don't have to do so many changes.