Skip to content

Instantly share code, notes, and snippets.

@markerikson
Last active June 5, 2016 22:30
Show Gist options
  • Save markerikson/15b35f1dde849372148b798c7961267e to your computer and use it in GitHub Desktop.
Save markerikson/15b35f1dde849372148b798c7961267e to your computer and use it in GitHub Desktop.
Redux createReducer chat notes

[6:19 PM] acemarke: the important thing is that when you use combineReducers, you're defining the key names, and which function should handle the portion of state at that key
[6:19 PM] Nigel: oh
[6:19 PM] Nigel: okaaay
[6:19 PM] acemarke: and when you use the object shorthand, like {change_points}, you're saying "create an object that has a key named 'change_points', and use the function named 'change_points' as the value"(edited)
[6:20 PM] acemarke: so if you want to use state.points as the name for that chunk of data, you have to be consistent in how you define it and how you access it
[6:21 PM] acemarke: and, when your change_points() function gets called, it will only be given the portion of data attached to that key. so, if it initially returns state = 0, then next time it's called, it will only be given 0 again, not {points : 0}
[6:21 PM] acemarke: it doesn't even know it's attached to a key named "points"
[6:21 PM] acemarke: does that make sense?
[6:23 PM] Nigel: Okay thx a lot, I didn't understand that combineReducer was attaching a reducer to a portion of the state object ^^
[6:24 PM] acemarke: yeah, that's the whole point
[6:24 PM] acemarke: you really only have one reducer function
[6:24 PM] acemarke: you could put every last bit of reducer code into that one function, and handle every single action
[6:24 PM] acemarke: but that would get long and hard to read
[6:25 PM] acemarke: so, programmers always break up bigger code into smaller code, usually with functions, right?
[6:25 PM] acemarke: so combineReducers is a utility to delegate the work, so that smaller functions handle specific smaller pieces
[6:25 PM] acemarke: I would actually clarify your last comment a bit:
[6:26 PM] acemarke: combineReducers isn't actually attaching functions to your state object itself
[6:26 PM] acemarke: rather, it's setting up a reducer structure
[6:26 PM] acemarke: and since most of the time reducers return a default value when first called, that initializes your state by returning new objects
[6:26 PM] acemarke: and produces the state structure you want
[6:26 PM] acemarke: slight difference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment