JS-Redux-Handle An Action

/*Handle an Action*/

const defaultState = {
  login: false
};

const reducer = (state = defaultState, action) => {
  return action.type==='LOGIN' ? state={login:true} : state
};

const store = Redux.createStore(reducer);

const loginAction = () => {
  return {
    type: 'LOGIN'
  }
};

/*Switch Statement to Handle Multiple Actions*/
const defaultState = {
  authenticated: false
};

const authReducer = (state = defaultState, action) => {
 switch(action.type) {
    case 'LOGIN': 
      return {authenticated: true};
    case 'LOGOUT': 
      return {authenticated: false};
    default: 
      return state;
    }
};

const store = Redux.createStore(authReducer);

const loginUser = () => {
  return {
    type: 'LOGIN'
  }
};

const logoutUser = () => {
  return {
    type: 'LOGOUT'
  }
};

Comments

Popular posts from this blog

Tech Duos For Web Development

CIFAR-10 Dataset Classification Using Convolutional Neural Networks (CNNs) With PyTorch

Long-short-term-memory (LSTM) Word Prediction With PyTorch