Created
January 25, 2016 14:08
-
-
Save sallar/80a4048358d77410d4ab to your computer and use it in GitHub Desktop.
Redux Reducers Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function reducer(state = []) { | |
return [...state, { | |
title: "Test", | |
completed: false | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)