Reducers - gabrielrangel95/react-native-folder-structure GitHub Wiki

Ex: <user.ts>

import { Types } from '@redux/types';
import { AnyAction } from 'redux';
import {
  IStateToProps,
  ITryLoginSuccess,from '@interfaces/user';
import { IGenericErrorAction } from '@interfaces/action';

const initialState: IStateToProps = {
  userData: undefined,
  loading: false,
  errorOnLogin: undefined,
  loginSuccess: undefined,
};

export default function user(state: IStateToProps = initialState, action: AnyAction){
  switch(action.type) {
    case Types.USER_TRY_LOGIN_REQUEST:
      return { ...state, loading: true, loginSuccess: null, errorOnLogin: null};
    case Types.USER_TRY_LOGIN_SUCCESS:
      return {
        ...state,
        userData: (action as ITryLoginSuccess).payload.user,
        errorOnLogin: undefined,
        loading: false,
        loginSuccess: true,
      };
    case Types.USER_TRY_LOGIN_FAILURE:
      return {
        ...state,
        userData: undefined,
        errorOnLogin: (action as IGenericErrorAction).payload.message,
        loginSuccess: false,
        loading: false,
      };
    default:
      return state;
  }
}

Ex: <index.ts>

import { combineReducers } from 'redux';
import user from './user';

export default combineReducers({
  user,
});

⚠️ **GitHub.com Fallback** ⚠️