Professional Documents
Culture Documents
When you need to calculate something based on Instead, calculate it during rendering phase
props or state change, don’t put it inside useEffect.
@_georgemoller
<Modal <Modal
state of <Modal
changes
(state) (state)
useImperativeHandle()
@_georgemoller
2
useImperativeHandle()
useImperativeHandle function.
@_georgemoller
@_georgemoller
R e act h o o ks .
useId
accessibility attributes.
data.
2
useEffectAfterMount()
dependencies change.
...DEPENDENCIES ON ARRAY
@_georgemoller
useEffect useLayoutEffect
component component
browser repaints
useEffect runs
the screen
@_georgemoller
@_georgemoller
useEffect cheatsheet
useEffect(() => {
useEffect(() => {
useEffect(() => {
/*...*/
/*...*/
/*...*/
Syncs with all data, Syncs with state & Syncs with no data,
runs after every props, runs after any runs only once after
R e act h o o ks .
useDeferredValue
of a state value
component that
Performs an expensive
computation
search term.
@_georgemoller
R e act h o o ks .
Info
search deferredSearch
s
“ “
se
s
sen
se
sene
sen
seneg
sene
senega
seneg
senegal
senega
senegal senegal
@_georgemoller
R e act h o o ks .
useDeferredValue
The Solution
How it works
R e act h o o ks .
useDeferredValue
The Problem
changes.
update immediately.
Thinking in
<Modal <RadioButton
Search for investors
<Modal.Header <SearchField
Name Has access?
<List
<Modal.Body
Michael Scott
<ListHeader
<Modal.Footer Pam Beasley
@_georgemoller
@_georgemoller
Modal Component
User clicks on open modal (React calls modal component with (React modifies the dom)
modal DOM
Open modal
modal
isOpen = true
setIsOpen(true)
Redux data flow
Open Sidebar
switch (action.type) {
dispatch(setIsSidebarOpen(true))
case SET_IS_SIDEBAR_OPEN:
return {
sidebar }
action creator
Updates store
return {
type: SET_IS_SIDEBAR_OPEN,
payload: isOpen
store
}
(global state)
/***/
isSidebarOpen: true,
/***/
@_georgemoller
React state tips!
Group related state
If two or more state variables are likely to
change together, then you should consider
putting those variables into an object.
@_georgemoller
@_georgemoller
React state tips!
Avoid redundant state
Calculations from props or state should not be put in
the component’s state.
React state tips!
Resetting a component’s
state with a key
Whenever you pass in a different key to a
component you are resetting the
component’s state.
@_georgemoller
@_georgemoller
React ref tips!
Don’t write/read a ref.current
during rendering.
Calculations from props or state should not be put in
the component’s state. You can read or write refs from event
handlers or effects instead.
@_georgemoller
src
assets assets folder contains all static files such as images, fonts etc.
utils everything that is shared between components or features, e.g. functions, hooks, etc.
features components that represent a feature should live here, alongside their tests, styles, etc.
components truly generic, reusable components, usually atoms and molecules like buttons, dropdown.
locales holds internationalization config, entries and messages for each language.
router.js entry point for routes, this file is a small wrapper around routes to delegate routes usage.
src
PostsList.styles.js
tests
httpClient.js
index.js
__tests__
@_georgemoller
src
Auth.js entries
Post.js
en-US.js
es-ES.js
messages
en-US.js
es-ES.js
index.js
georgemoller2106 @_georgemoller
React
State
useState()
useReducer()
with a reducer.
Context
useContext()
useEvents()
Extract non-reactive effect logic into an
event function.
Transitions
useDeferredValue()
Defer to more urgent updates.
useTransition()
Lets you mark updates in the provided
callback as transitions.
georgemoller2106 @_georgemoller
React
Accessibility
useId()
client.
Debugging
useDebugValue()
Refs
useRef()
Declares a ref.
useImperativeHandle()
parent refs.
georgemoller2106 @_georgemoller
React
Memoization
useCallback()
useMemo()
Effects
useInsertionEffect()
Subscribing
useSyncExternalStore()
Effects
useEffect()
useLayoutEffect()
Components Flow
Trigger a Render
Render
React Renders Your
component/s React calls your component/s
Commit
React commits
changes to the DOM React updates the DOM
Browser
Rendering Browser paints the changes on the screen
@_georgemoller
src
components
Button
tests
useCallback()
Bad use case Good use case
@_georgemoller
React
4 possible combinations
1 imposible state
@_georgemoller
React
‘empty’ or ‘typing’.
How useMemo() works
in React?
useMemo will only recompute a value
when one of the dependencies has
changed.
@_georgemoller
@_georgemoller
Introduction to useMemo()
When to useMemo()?
You have an expensive function on a
1 component that often re-renders (due to
props or state change)
useMemo() dependencies
Be careful of the dependencies you pass into the useMemo hook. If
these always change between re-renders, it defeats the purpose of
using useMemo().
@_georgemoller
React components.
user.
expose them.
@_georgemoller
React components.
React components.
extension.
@_georgemoller
React components.
options.
@_georgemoller
React components.
React components.
extension.
@_georgemoller
United States
Australia
Austria
Azerbaijan
@_georgemoller
<Dropdown
<Toggle
United States
Build a dropdown
Australia
UI using React <Item
Austria
Components Azerbaijan
<Menu
@_georgemoller
Build a dropdown
UI using React
Components
@_georgemoller
1
Identify logic that Logic that can be reused
can be reused
@_georgemoller
2
Extract that logic into
a custom hook
@_georgemoller
3
Call the custom hook from components
useCallback() in React?
useCallback() useMemo()
useCallback will remember a function between renders. useMemo will remember a value between renders.
Used to fix performance issues, e.g. when changing a Used to avoid re-calculating expensive operations.
function can cause expensive components to re-render.
@_georgemoller
Instead do:
users.length > 0.
Avoid this useEffect()
mistake
parent component.
pass.
@_georgemoller
Instead notify of the state
handler.
@_georgemoller