Reputation: 22520
I am getting my head around redux/react but having issues with my reducer:
const reducer = (state = 0, action) => {
switch (action.type) {
case "INCREMENT":
return state++;
default:
return state;
}
};
Trying to inject this into my store:
const store = createStore(reducer);
How can I avoid this error?
Upvotes: 0
Views: 60
Reputation: 2302
Thanks for the sandbox!
Your "reducer" variable isn't defined when createStore
is executed. Either 1) define it before you use it, or 2) define it with a function declaration:
function reducer(state = 0, action) {
switch (action.type) {
case "INCREMENT":
return state++;
default:
return state;
}
}
which causes it to be "hoisted" (See "Function Declaration Hoisting").
After that, you'll notice that you get a subsequent error:
Expected listener to be a function.
on store.subscribe(App);
. This is because class declarations aren't hoisted (see "Hoisting"). So you'll want the class declaration of App
before store.subscribe(App);
.
Upvotes: 3