10. HOW TO SHARE LOGIC
BETWEEN COMPONENTS?
Tony Lee
TEXT
11. REUSING LOGIC
HIGHER-ORDER COMPONENTS
▸ A function that takes a component
and returns a new component
▸ e.g. React-Redux’s connect function
<ComponentWhichImplementsLogic>
<ComponentWhichUsesLogic />
<ComponentWhichImplementsLogic>
ComponentWhichImplementsLogic(
ComponentWhichUsesLogic
)
withMousePosition(withClock(MyComponent))
withClock(MyComponent)
12. REUSING LOGIC
RENDER PROPS
▸ Using a prop whose value is a
function.
▸
<Clock
render={props => <MyComponent {...props} />}
/>
<Clock render={({time}) => (
<MousePosition render={({x, y}) => (
<MyComponent time={time} x={x} y={y} />
)} />
)} />
15. GIANT COMPONENTS
▸ The logic is split across a lot of different lifecycle methods
▸ Duplicate code for different lifecycle methods
▸ Thousands of lines code
17. SHOULD I USE CLASS
COMPONENT OR FUNCTION
COMPONENT?
Rajalakshmi Somasundaram
TEXT
18. CONFUSING CLASSES
▸ Use class component to access state and life cycles
▸ Have to convert your function component to class
component if you add some state (Why not use class
component by default)
▸ Don’t forget `bind(this)` 😄
20. Functions which can only be used from inside
Functional Components or Other Hooks
Named useXXX()
Use state, life cycles and other features
without writing a class
Hooks are highly re-usable
and independent for each component
31. useReducer
▸ useReducer + Context API = redux
▸ when you have complex state logic that involves multiple sub-values
or when the next state depends on the previous one.
▸ 《Use Hooks + Context, not React + Redux》
61. WHY DO WE NEED REDUX FOR
SUGGESTIONS?
Yizhi Zhou
TEXT
62. TEXT
WHY DO WE NEED REDUX?
▸ Communication between
components
▸ e.g. Input ->
SuggestionsList
▸ Dealing with async
▸ e.g. Ajax call
▸ Redux Dev Tool
63. COULD WE GET RIDE OF
REDUX FOR SUGGETIONS?
Tony Lee
TEXT
65. TEXT
DEALING WITH ASYNC
▸ We can’t use async/await in a render function. 😢
let loading = false;
const suggestions = await fetchSuggestions(query);
loading = false;
66. TEXT
DEALING WITH ASYNC
▸ But we can use custom hooks. 😄
const [query, setQuery] = useState('');
const {loading, suggestions} = useSuggestions(query);
if (loading) {
return 'loading...'
}
return (
<ul>
{suggestions.map(renderSuggstion)}
</ul>
)
75. HOOKS VS REDUX
HOOKS
▸ Light
▸ New APIs for function
components
▸ useReducer + Context works
like redux
▸ React Dev Tool
REDUX
▸ Powerful
▸ State management
framework
▸ React-Redux has hooks API,
useSelector, useDispatch
▸ Redux Dev Tool