#ScottishSummit2021
M a u r i c e d e B e i j e r
W h y I a m h o o k e d o n t h e f u t u r e o f R e a c t
S a t 1 5 : 0 0 — 4 5 m i n
Our Sponsors
T h e P r o b l e m S o l v e r
F o u n d e r / T r a i n e r / L e a d
@ m a u r i c e d b
Maurice
De Beijer
Agenda
Future
• Concurrent Rendering
• useTransition()
• <SuspenseList />
• Server Components
Today
• Asynchronous Data:
• Class based components
• Functional components
• Component hooks
• Resources
• <Suspense />
#ScottishSummit2021
Used to be the most common way to write React components
Class
Components
Houston we
have a
problem!
The problem
with classes
Fat arrow for some functions!
But not for other functions? 🤔
Fat arrows
If all you have is a
hammer
Scottish
Summit
Template
Then every problem looks like a nail
Deeply Nested
Components
Goes Here
Scottish
Summit
Template
Basic
ComponentHo
oks
Scottish
Summit
Template
• Manage state in a functional component
• Returns a tuple with:
• The current state
• An updater function
useState()
hook
• Manage side effects in a functional component
• Takes a function that contains the side effect
• Optional: Can return a clean-up function
👉 Always use the second argument with dependencies
useEffect()
hook
React Context
Scottish
Summit
Template
It’s all a matter of perspective
A flexible but hard to use pattern 😢
Render Props
Returns the current context value for a given context
useContext()
hook
Other hooks
Scottish
Summit
Template
• useReducer is an alternative to useState
and accepts a reducer function
• useCallback returns a memoized
callback
• useMemo returns a memoized value
• useRef returns a mutable ref object
whose .current property is initialized to
the passed argument
Additional
Hooks
• useImperativeHandle customizes the instance
value that is exposed to parent components
when using ref
• useLayoutEffect is like useEffect, but it fires
synchronously after all DOM mutations
• useDebugValue can be used to display a label
for custom hooks in React DevTools
Additional
Hooks
Custom hooks
Scottish
Summit
Template
Reuse shared logic between components
Custom hooks
Simplify the functional component even more
Hooks can’t be used in class based components 😢
Using the
custom hook
Rules of hooks
Scottish
Summit
Template
Data Fetching
using Suspense
Scottish
Summit
Template
• Load asynchronous data
• Suspends rendering the closest <Suspense> component
• 💡 Throw a promise to suspend!
See this gist from Ryan Florence
Resource
Manager
A resource using fetch() to load some data
AJAX
Resource
• Fetching asynchronous data
• Suspends rendering the closest <Suspense> component if
not ready
👉 Use an <ErrorBoundary> to handle errors
Fetching the
resource
<Suspense />
Goes Here
Scottish
Summit
Template
• Suspend rendering when one or more of the child
components is not ready
• Display a fallback component instead
👉 Handle errors the standard React way
<Suspense />
What is next
for React?
Scottish
Summit
Template
Concurrent
Rendering
• Changes how React renders your components
• The render phase is split into chunks and can be
interrupted/aborted and restarted
• The commit phase is still atomic
• No partial screen updates
Concurrent
Rendering
While React is rendering components
User clicks
When React is completely done rendering
The event
executes
React rendering is chunked into slices
Concurrent
mode
When a slice is done
• It can invalidate the render so far and restart it
• Render functions will be called multiple times
The event
executes
useTransition()
Scottish
Summit
Template
Transition between application states in a more controlled
manner
useTransition()
<SuspenseList />
Scottish
Summit
Template
• Control how multiple <Suspense> boundaries are rendered
• For example:
Prevents multiple loading indicators at the same time
<SuspenseList />
• They only run on the server, not on the client
• Can use a database, file or similar resource
• Can’t use hooks like setState or useEffect
• Can render other server or client components
• Client components can only render other client
components
Server
Components
Scottish
Summit
Template
Server components only run on the server
They can use a database, file or similar resource
But they can’t use hooks like useState or useEffect
They can render other server or client components
Client components only run on the client
They can only render other client components
Regular components can run on both client and server
Depending on who renders them
Server
Components
• This isn’t server side rendering
Server components send a special serialization format
• SSR would return HTML to the client
Server Side
Rendering?
#ScottishSummit2021
Thank You

Why I am hooked on the future of React