Why do i get Error: Reducer "age" returned undefined during initialization .. What am i doing wrong?

3335 views redux
2

I am new to react. I created quick and dirty test code just to test out the combinereducers funcation. But when I run it .. i keep getting this error message

Error: Reducer "age" returned undefined during initialization

I am not sure what I am doing wrong here. Below is a copy of of my code. Any help would be really appreciated

Error: Reducer "age" returned undefined during initialization

import {combineReducers} from 'redux';

const initial_state = {

    age: 32

}

 const age = (state = initial_state, action) => {

    if(action.type === "ADD"){


        return {...state,age: state.age+=1 }

    }
}

 const age2 = (state = initial_state, action) => {

    if(action.type === "ADD"){


        return {...state,age: state.age+=1 }

    }


}

const root_reducer = combineReducers({age,age2});
export default root_reducer;

answered question

1 Answer

2

what about returning initial state in age?

const age = (state = initial_state, action) => {
    if(action.type === "ADD"){
        return {...state,age: state.age += 1};
    }

    return initial_state;
};

posted this

Have an answer?

JD

Please login first before posting an answer.