ReducerPattern
ReducerPattern
explain that the video in the TK talks about Redux but we haven’t covered Redux. explain that the REducer pattern is used in Redux and that’s why.
Talk about immutability and why it’s important
repl.it project
build a new ES6 repl.it project.
show const and let for numbers
show const and let for objects
show how you can mutate objects
show how even with the const keyword. show that there is no const for keys on objects
show how “copying” an object by equal assign mutates the original
draw out how memory works for these variables and explain why mutation of original in object and the const keyword is possible because of how things are stored in memory
codesandbox project
use the codesandbox title project
explain pure functions
no side effects
exact same output every time with exact same inputs
explain reducers
pure function
takes two objects and “reduces” them to one
for our purposes, reducers take two values, and state object and an action object
explain an action
object that has a requires ‘type’ key
has an optional ‘payload’ key (for when you need it)
build a titleReducer
create and export initialValue
create and export the reducer funtion
in Title.js comment out the useState for title and editing and replace with reducer
refactor code to use properly. ignore dispatch for now
go over dispatch
create an action and code path in reducer for ‘TOGGLE_EDITING’
dependingo n time, maybe breakout here and have groups do ‘SET_TITLE’
when they get back look at some code and build it out both with two dispatches on button press as well as one with reducer of SET_TITLE also setting the editing kay
rejoice
Last updated