For the time being I can't use ES6/ES2015 and I'm stuck with ES5 for writing Redux reducers. Since the state parameter of a reducer has to be immutable, even when it's undefined, I came up with the following pattern:
function myState( state, action ) {
if ( typeof state === 'undefined' ) {
return myState( { value1: 'foo', value2: 'bar' }, action );
}
// actual state calculation here
}
Any alternative suggestions or comments on how to ensure a default value with ES5?
Edit: After some questions and suggestions: the reason I do a recursive call is that I take the "state is immutable" very seriously. So even when the state
parameter is undefined
, I don't change the parameter variable itself. Am I taking the immutability too far?
Redux doesn’t force you to use the default argument syntax. It only cares that when it gives you
undefined
as the state, you return something else so that your app is able to boot up with an initial state tree.This function in ES6:
Is equivalent to this function in ES5:
A nice way to verify this is to run this code through the Babel REPL.
There is no need for a recursive call here. I think your question may contain some confusion about the difference between mutation and reference assignment.
When you write
you are mutating the
x
object. This is what Redux doesn’t allow.However when you write
the original object stays intact.
x
“binding” (also known as a “variable”) just starts pointing at a different object.Redux doesn’t care if you change what
state
argument refers to. It is local to your function. Whether you return it or not, changing the reference fine as long as you don’t mutate the actual objects or any objects inside it.Just changing what the variable refers to does not mutate the objects:
However changing something inside the object itself, or objects it links to, whether deeply or not, is a mutation, and is not allowed by Redux:
Another option would be something like this: