Skip to content

Instantly share code, notes, and snippets.

@sallar
Created January 25, 2016 14:08
Show Gist options
  • Save sallar/80a4048358d77410d4ab to your computer and use it in GitHub Desktop.
Save sallar/80a4048358d77410d4ab to your computer and use it in GitHub Desktop.
Redux Reducers Example
function reducer(state = []) {
return [...state, {
title: "Test",
completed: false
}];
}
@SebastienDaniel
Copy link

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