CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
React Redux Immutable Game
1. React with Redux (& Immutable)
Adding Redux to a React App
@tinaheiligers1, tina.heiligers@gmail.com, https://github.com/TinaHeiligers
2. The next 40-45 min….
...is best if you have some background in React and Redux
…is even better if you’ve worked with Immutable
...understand that I’ll probably make mistakes and stumble over my words
...don’t mind a South African accent
The what, the why, the how….final result!
3. The app...
● Game (match cards)
● Aimed at kids (expect to see cartoons)
● Versions:
○ React v15
○ React v16 + Redux (+ redux saga) + Immutable
● Subtle differences: styling, routing, layout
6. React app Architecture
Parent (App container):
● State -> props to children
● Methods -> props to children
● Renders children
Props, methods from App ->
components
Sta
Sta
na
n
11. Start page: Overview Sta /da & me d r A p
New t /da s e b t A
Gra t om
us , se t
ba t A , no
we p a r
e r xi ~> ro g
12. Deep nesting: Prop drilling!
Passing methods down to children components
Passing method results back up to parent component
What’s wrong with React on its
own?
React
Parent
State Methods
Child
Props Props()
Nothing (if components are shallowly nested)
15. Why use Redux?
React with Redux
Store
Container
Component
Shared (redux) state between components
Modular approach: React (UI), Redux (State)
Simplify logic
Easy to move logic around
-> no/minimal decoupling
16. Redux
Con t l et t am u t a g in ;
Don’t a r ut ;
Don’t e n ex n ta ,
https://redux.js.org/
?
33. My Approach
Decide: Rewrite vs Refactor
Rewrite
Decouple complex interdependent logic
Limited test coverage
Small apps easier, take less time
Refactor
Redefine logic
Reasonable test coverage
Larger apps (already in prod?)
Iterative approach
34. My Approach
Think twice, act once!
1. Understand the code!
2. Decouple the logic
3. ‘Design’ your state (not always a
1 to 1 mapping)
4. Which components need data
from state
5. Tackle one feature at a time
35. My Approach
Step 1: Understand
Understand the code!
...we’ve t e t ....(ki )
36. My Approach
Step 2: Decouple Logic
● UI methods -> React/Utilities
methods
○ Date-time methods
○ Time outs
○ Animations
○ etc.
● Data methods -> Redux state
○ API interactions (typically)
○ Data manipulations
○ Error methods
○ Side effects
37. My Approach
Step 3: Design state
● UI state -> React state
○ Toggles, sliders, colors,
hidden/visible etc.
● Data state (+ manipulators) ->
Redux state
○ API interactions (typically)
○ Form data
38. My Approach
Step 4: Redefine components
What data does a component need?
-> map state to props
How does a component interact with
data? -> map dispatch to props
Which components are purely views?
39. To Immutable or not?
The case for immutable
Pros:
Protects from mutations
Protects you from yourself!
Cons:
Learning curve
Verbose
The case against immutable
Pros:
Nothing new to learn
Concise
Cons:
No protection from mutations
Discipline
41. Features: still the same
Players (names,
matched cards)
Cards (visible,
selected, matched)
Player turn
Game over
Game (not started, in progress, over)
Layout is slightly different