-
Resuing logic HOC, Render props => Wrapper hell, difficult for data flow throw the app You need stateful logic
-
Giant components: logic has to be distributed in [Screenshot] multiple lifecycles
-
Classes. Aaah!!!! i) Even if its a small component, you fear to create it as a functional component coz what if it needs state logic in future.
ii) Doesn't trash out unnecessary methods in compile time. Difficult to understand the scopes.
React doesn't provide a simpler solution for stateful logics
Reusable extensions which can be plugged into a callback/listener
================= React Hooks ===================
- useState
Demo: Checkbox.
- You need to bind handlers as well
- Now add one more field, is it getting worse?
================ render crop API for ThemeContext
- useContext
What is context? Context provides a way to pass data through the component tree without having to pass props down manually at every level
Compare process of using theme and locale in class/hooks
You need to end LocaleContext.Consumer which makes nested components
- useEffect
Problem? Component life cycles make you write behaviours in chunks in different methods. Makes it inconsistent. Difficult to understand, for testing, for debugging.
What if a single behaviour is only in one method?
How cleanup works? In useEffect(, [stateVariable])
Custom Hooks? useXyz() We want to pull out some generic behaviours from our components, and remove deduplication. Create a custom hook for it. Like blur effect. [Showcase]
Advantage: Custom hooks give you the ability to write your own abstraction, that simplifies your component tree and avoids wrappers
==================
useReducer
==================
Demo