Search code examples
reactjsreduxstatereduce

React-Redux add new value to the state


I am having a two step form in react. The first step of the form we ask some information to the user and then I add it to the state. The second step of the form I ask some more information to the user and add it to the state, so instead of appending the information that was asked on step 2 of the form, it overrides the state, so the state now only the info that was asked in the step 2 of the form. How can I add the have both the information together. When i try to ...state it gives me error as state is not iterable.

const infoReducer = (state = {}, action) => {
switch(action.type) {
    case 'STEP_1':
        return action.payload
    case 'STEP_2':
        return action.payload
    default:
        return state
}

}


Solution

  • check this out

    import { ActionTypes } from "../../Constant/ActionType";
    const initState = {
      Auth: {},
    };
    const AuthReducer = (state = initState, action) => {
      switch (action.type) {
        case ActionTypes.AuthUser:
          return {
            ...state,
            Auth: action.payload,
          };
        default:
          return state;
      }
    };
    export default AuthReducer;