handleActions returning defaultState even when state passed is not undefined
See original GitHub issueimport { handleActions } from 'redux-actions';
const getInitialState = () => {
return ['red'];
};
const reducer = handleActions({
test: (state, action) => {
return action.payload.a ? undefined : ['yellow'];
},
test2: (state) => {
return ['1'];
}
}, getInitialState());
const reducer2 = handleActions({
test: (state, action) => {
return action.payload.a ? undefined : ['yellow'];
}
}, getInitialState());
console.log(reducer(['green'], {
type: 'test',
payload: {
a: 1
}
}));
// ['red']
console.log(reducer2(['green'], {
type: 'test',
payload: {
a: 1
}
}));
// undefined
The first console statement returns the defaultState which is ['red']
whereas the second console statement returns undefined
even though they both respond to the same action and have same action handling logic. Can someone tell me why is this happening?
Issue Analytics
- State:
- Created 7 years ago
- Comments:22 (1 by maintainers)
Top Results From Across the Web
handleAction(s) - redux-actions
The third parameter defaultState is required, and is used when undefined is passed to the reducer. EXAMPLE. handleAction(. 'APP/COUNTER/INCREMENT',. (state ...
Read more >How to reset the state of a Redux store? - Stack Overflow
Our fix was to not set the state to undefined but rather still use the ... you call RESET action, you reducer will...
Read more >Why is my useSelector returning undefined but my state is ...
This mutates the state. Never do that! The whole point of a reducer is that it returns a new state and does not...
Read more >redux-standard-actions - npm
Returns an object mapping action types to Flux Standard Action creators. ... a default state which is used when an undefined state is...
Read more >Redux update an item in an array of objects
If a Redux reducer directly mutates, and returns, the state object passed into it, ... objects, arrays, maps, sets or even custom defined...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The issue is not with this library, it’s actually because of
reduce-reducers
which is being used byhandleActions
.reduce-reducers
reduces multiple reducers into one reducer by the following codeSo the reason why test case A fails is because since the first reducer inside
handleActions
returnsundefined
, the second reducer will getundefined
as the initial state and will return thedefault state
. But if we push thetest
reducer to the end insidehandleActions
, then it will receive the actual state and will returnundefined
. So the following test case will pass.So I don’t think there is anything to fix here since reducers should not be returning undefined in the first place, but it’ll be good if an error is thrown when a reducer returns
undefined
so that the user knows there is a problem, otherwise the reducer might return incorrect state which might lead to unexpected results in the application@amit1911 thanks for doing the digging here. Are you still interested in implementing a PR that does error checking for cases like this, or shall I try my hand at it?