I am trying to add immer to a react native expo app with redux.
I am getting this error:
React Native - Immer with Expo.io Reducer “user” returned undefined during initialization
Before Immer (working)
import { LOGIN_ACTION } from '../actions/actionTypes';
const initialState = {
user: false,
};
const User = (state = initialState, action) => {
switch (action.type) {
case LOGIN_ACTION:
return {
...state,
'user': action.user
};
default:
return state;
}
};
export default User;
After Immer (not working)
import produce from 'immer';
import { LOGIN_ACTION } from '../actions/actionTypes';
const initialState = {
user: false,
};
const User = (state = initialState, action) => {
produce((state, draft) => {
switch (action.type) {
case LOGIN_ACTION:
draft.user = action.user;
break;
}
});
};
export default User;
What am I doing wrong?
Here is the full error I am getting:
Reducer "user" returned undefined during initialization. If the state passed to the reducer is undefined, you must explicitly return the initial state. The initial state may not be undefined. If you don't want to set a value for this reducer, you can use null instead of undefined.
redux.js:366:22 forEach [native code]:0 assertReducerShape redux.js:359:32 combineReducers redux.js:422:23 index.js:4:31 loadModuleImplementation require.js:292:12 index.js:2 loadModuleImplementation require.js:292:12 App.js:5 loadModuleImplementation require.js:292:12 AppEntry.js:2 loadModuleImplementation require.js:292:12 guardedLoadModule require.js:179:45 global code :0
Should work like react! I followed this example:
https://github.com/react-boilerplate/react-boilerplate/blob/master/app/containers/HomePage/reducer.js
return produce(....
Op di 30 jul. 2019 03:10 schreef tomer-tgp notifications@github.com:
🐛 Bug Report
I am trying to add immer to a react native expo app with redux.
I am getting this error:
React Native - Immer with Expo.io Reducer “user” returned undefined during
initialization
To ReproduceBefore Immer (working)
import { LOGIN_ACTION } from '../actions/actionTypes';
const initialState = {
user: false,
};
const User = (state = initialState, action) => {
switch (action.type) {
case LOGIN_ACTION: return { ...state, 'user': action.user }; default: return state;}
};
export default User;
After Immer (not working)
import produce from 'immer';
import { LOGIN_ACTION } from '../actions/actionTypes';
const initialState = {
user: false,
};
const User = (state = initialState, action) => {
produce((state, draft) => {
switch (action.type) { case LOGIN_ACTION: draft.user = action.user; break; }});
};
export default User;
What am I doing wrong?
Here is the full error I am getting:
Reducer "user" returned undefined during initialization. If the state passed to the reducer is undefined, you must explicitly return the initial state. The initial state may not be undefined. If you don't want to set a value for this reducer, you can use null instead of undefined.
redux.js:366:22 forEach [native code]:0 assertReducerShape redux.js:359:32 combineReducers redux.js:422:23 index.js:4:31 loadModuleImplementation require.js:292:12 index.js:2 loadModuleImplementation require.js:292:12 App.js:5 loadModuleImplementation require.js:292:12 AppEntry.js:2 loadModuleImplementation require.js:292:12 guardedLoadModule require.js:179:45 global code :0
Expected behavior
Should work like react! I followed this example:
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/immerjs/immer/issues/399?email_source=notifications&email_token=AAN4NBBJRJRS4TMANRTNIELQB6IHBA5CNFSM4IHXOLZ2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HCFSY4Q,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAN4NBAO6TUPIEZG2N6GECTQB6IHBANCNFSM4IHXOLZQ
.
Thank you! I missed it because of the implicit return of es6 arrow function.
Most helpful comment
return produce(....
Op di 30 jul. 2019 03:10 schreef tomer-tgp notifications@github.com: