Slides
Slides
Main Job: Render UI & React to User Input Side Effects: Anything Else
Sometimes, you have more complex state – for example if it got multiple
states, multiple ways of changing it or dependencies to other states
Generally, you’ll know when you need useReducer() (è when using useState()
becomes cumbersome or you’re getting a lot of bugs/ unintended behaviors)
useState() useReducer()
The main state management “tool” Great if you need “more power”
Great if state updates are easy and limited to Can be helpful if you have more complex
a few kinds of updates state updates
Component Trees & Component Dependencies
Instead: Use
Props & Functions
passed via Props <App />
Login
<Product />
Add to Cart
There is no direct There is no direct
connection connection
Context to the Rescue!
Component-wide, “behind-the-scenes” State Storage
<App />
Login
<Product />
Add to Cart
There is no direct There is no direct
connection connection
Context Limitations
Only call React Hooks in React Only call React Hooks at the Top
Functions Level
+ extra, unofficial Rule for useEffect(): ALWAYS add everything you refer to
inside of useEffect() as a dependency!