I read on redux-devtools
walkthrough:
Your reducers have to be pure and free of side effects to work correctly with DevTools. For example, even generating a random ID in reducer makes it impure and non-deterministic. Instead, do this in action creators.
Those words says about generating random value. But, how about taking unique values based on current state? For example, by taking array's length as an ID for elements when storing data to current state. As long as I'm not removing any value of the array, it guaranteed to be unique. Do this makes a function impure?
Or, by creating conditional value based on current state like this:
function reducer (state, action) {
if (state.isLocked) return state;
// calculate further
}
Or, more extreme, by having a defined value that only exist inside a function like:
{ type: 'CREATE_USER', age: 13 }
// it is predictable, always return the same value with same argument
function getAgeText(age) {
if (age < 5) return 'baby';
if (age < 20) return 'teenager';
if (age < 30) return 'mature';
if (age < 40) return 'old';
if (age < 50) return 'very old';
return 'astounding';
} // of course by assuming we have to use this function in case you
// ask, "Why don't get those values anywhere else and not in
// this reducer?"
function reducer (state, action) {
switch (action.type) {
case 'CREATE_USER':
return Object.assign({}, state, {
age: age, ageText: getAgeText(action.age)
});
default:
return state;
}
}
So, is those examples make function impure? And, if not, is it a bad practice in redux to create a long, complex calculation or heavily nested reducer even if it is done only from the passed value (state and action)?