Skip to content

Instantly share code, notes, and snippets.

@sallar sallar/reducer.js
Created Jan 25, 2016

Embed
What would you like to do?
Redux Reducers Example
function reducer(state = []) {
return [...state, {
title: "Test",
completed: false
}];
}
@SebastienDaniel

This comment has been minimized.

Copy link

commented Jan 25, 2016

You're returning a NEW array (new reference because data changed)
But most internal objects are references to the ones provided in the state argument (spread operator (...)), and you add a new object to the array.

So you only created a new array, kept reference of all previous data, and created a new object within the array.

The "new array" is created to change the reference, allowing most store subscribers to "react" by simple comparison (if (previous !== new) do something), so you don't have to do deep comparisons, which can be costly.
Redux + Immutable data structures are FAST, because they work via reference instead of deep copying and deep comparisons (which are the costly bits in most UI-centric apps)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.