user18658903
user18658903

Reputation: 11

Expected the root reducer to be a function. Instead, received: 'undefined'

I keep getting this error

"throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(2) : "Expected the root reducer to be a function. Instead, received: '" + kindOf(reducer) + "'");"
const redux = require('redux');
const createStore = redux.createStore()
const combineReducers = redux.combineReducers()

// Action Creators

function orderCake() {
    return {
        type: 'CAKE_ORDERED',
        payload:1
    }
}


// States
const initialStateCake = {
    numberOfCakes : 10
}





const cakeReducer = (state = initialStateCake, action) => {
  switch(action.type) {
        case 'CAKE_ORDERED':
        return {
            ...state,
            numberOfCakes: state.numberOfCakes - action.payload
        }
        default: 
        return state
  }
}




const store = createStore(cakeReducer) 
store.dispatch(orderCake())



console.log("Initial State", store.getState())

Upvotes: 1

Views: 616

Answers (3)

Bashid
Bashid

Reputation: 510

easy mistake, below line

const createStore = redux.createStore()

should be

const createStore = redux.createStore

or

const createStore = redux.legacy_createStore

Upvotes: 0

Nirmal Sankalana
Nirmal Sankalana

Reputation: 443

I also followed the same tutorial and got an error like this. The second line of code is wrong because redux.createStore is not a function, it is a property of the redux object. To create a store with redux, the correct code would be:

const redux = require("redux");
const createStore = redux.createStore;
const store = createStore(reducer);

If you are interested you can see the work which I have done when doing this tutorial here

Upvotes: 0

user2285798
user2285798

Reputation: 31

I found solution here, just remove the parenthesis from createStore

https://www.youtube.com/watch?v=lxWy9Nz63to&ab_channel=UnitedTopTech

Upvotes: 2

Related Questions