What's new in React 19 | Lydia Hallie

React Conf
25 Jul 202420:03

Summary

TLDRIn this talk, Lydia introduces React 19's new features, focusing on transitions for responsive UI updates and the useActionState hook for streamlined state management. She also covers React Server Components for efficient server-side rendering and the use API for handling asynchronous data on the client side. Additionally, she discusses improvements like ref props in server components and browser hint APIs for optimized resource loading.

Takeaways

  • πŸ†• React 19 introduces stable features that were previously in the Canary version, focusing on improving the user experience and reducing boilerplate code.
  • πŸ”„ Transitions, introduced in React 18, are now stable in React 19, allowing for more responsive UI updates by yielding to the main thread every 5 milliseconds to handle user interactions.
  • πŸ“ˆ The useTransition hook in React 19 simplifies handling state updates by automatically setting the pending state and reducing the amount of boilerplate code needed.
  • πŸŽ›οΈ Actions in React 19, marked with 'useActionState', provide a way to handle transitions and state updates with improved status information and less manual intervention.
  • πŸ”— React 19 allows for server components with 'use client bundle' directive, enabling more direct server-side functionality within components without the need for HTML or JavaScript on the client side.
  • 🌐 Server components in React 19 can be used to access server-side resources like databases and file systems, and are sent to the client as JSON, streamlining the rendering process.
  • πŸ“š The 'useFormStatus' hook offers an easy way to get status information from the last form submission, improving form handling and user feedback.
  • 😌 The 'useOptimistic' hook allows for updating the UI optimistically before a transition completes, providing a smoother user experience by showing data as if the action has already completed.
  • πŸ“ˆ React 19 supports new APIs for working with browser hints like preload, prefetch, and DNS, which can help speed up resource loading times.
  • πŸ“ React 19 now supports rendering document metadata directly within components, simplifying the management of titles, links, and meta tags.
  • 🎨 The support for directly using stylesheets in components ensures that the browser does not render components until the CSS has loaded, preventing FOUC (Flash of Unstyled Content).

Q & A

  • What is the main focus of the talk by Lydia about React 19?

    -The talk by Lydia focuses on the new features and improvements in React 19, including transitions, the useActionState hook, server components, and other enhancements that make building with React more efficient and responsive.

  • How do transitions in React 19 help with UI responsiveness during state updates?

    -Transitions in React 19 help with UI responsiveness by yielding back to the main thread every 5 milliseconds to check for other tasks, such as user interactions, allowing React to pause the current render, handle the task, and then resume rendering, thus preventing the UI from becoming unresponsive.

  • What is the purpose of the useActionState hook introduced in React 19?

    -The useActionState hook simplifies the handling of asynchronous state updates by providing the action's state, the action function itself, and a pending state indicator, reducing the amount of boilerplate code needed to manage data, pending, and error states.

  • Can you explain the concept of actions in React 19 and how they differ from traditional state updates?

    -Actions in React 19 are functions that trigger transitions, which are asynchronous state updates. They differ from traditional state updates by allowing React to batch and commit changes more efficiently, ensuring a smoother user experience and reducing visual overwhelm during rapid state changes.

  • What is the significance of server components in React 19, and how do they differ from traditional client-side rendering?

    -Server components in React 19 allow React to send serialized componentry to the client in a special JSON-like format, which can be directly reconstructed on the client side without the need for HTML or JavaScript. This approach reduces the reliance on the client-side renderer to rebuild the React tree, streamlining the rendering process and improving performance.

  • How does the useClientBundling directive in React 19 help with client-side rendering of components?

    -The useClientBundling directive in React 19 adds the components and their imports to the client-side JavaScript bundle, instructing the bundler to include them in the client-side rendering. This allows components to be rendered on the client side without server-side rendering, providing more flexibility in component rendering strategies.

  • What is the useOptimistic hook in React 19, and how does it affect the UI updates?

    -The useOptimistic hook in React 19 allows developers to update the UI optimistically, showing data as if an action has already completed before the actual data is returned. This creates a smoother user experience by immediately reflecting changes in the UI and updating it once the data is confirmed.

  • How can the useFormStatus hook in React 19 be used to enhance form handling?

    -The useFormStatus hook in React 19 provides information about the last form submission, including the form data being submitted and the transition's pending state. This makes it easier to create reusable form elements and provide real-time feedback and status updates to users.

  • What are server actions in React 19, and how do they simplify server-side functionality within client components?

    -Server actions in React 19 are functions that run server-side but can be called from the client-side. They simplify server-side functionality within client components by offloading computations or data manipulations to the server, reducing the load on the client and eliminating the need for separate API endpoints.

  • How do the new APIs in React 19 for working with browser hints benefit performance?

    -The new APIs in React 19 for working with browser hints, such as preload, preconnect, and prefetch, allow developers to fetch or connect to certain resources before they are discovered, which can speed up loading times. React handles the hoisting to the head and automatically reorders the browser hints based on their priority, ensuring that the most critical resources are made available as quickly as possible.

Outlines

00:00

πŸš€ Introduction to React 19 Features

The speaker, Lydia, introduces the new features in React 19, emphasizing the stability of features previously in the experimental React Canary. Transitions, introduced in React 18, are highlighted as a method to maintain UI responsiveness during state updates. Lydia explains how transitions work by yielding to the main thread every 5 milliseconds, allowing other tasks like user interactions to be handled. This prevents the application from becoming unresponsive. She also discusses the use of the `useTransition` hook for handling state updates and user interactions more efficiently, reducing boilerplate code and improving the user experience.

05:01

πŸ”„ Enhancing User Experience with Transitions and useAction

This paragraph delves deeper into the use of transitions and the `useAction` hook in React 19. Lydia explains how `useAction` simplifies handling transitions by providing the action function and pending state. She refactors a code example to use `useAction`, eliminating the need for manual state management. Lydia also touches on React DOM's ability to work with actions through props, and introduces `useFormStatus` for easier access to form submission status. The paragraph concludes with the introduction of the `useOptimistic` hook, which allows developers to update the UI optimistically before a transition completes, providing a smoother user experience.

10:02

🌐 Embracing Server Components and Asynchronous Data Handling

Lydia discusses the introduction of React Server Components (RSC) in React 19, which enable a new rendering approach where React sends serialized components to the client in a JSON-like format. This allows for server-side functionality to be directly used within components. She explains the use of suspense boundaries to handle data fetching within server components and introduces the `use` API for handling promises and context more flexibly on the client side. The paragraph also covers the `useClientBundle` directive for adding components to the client-side bundle and the benefits of using the `use` API for asynchronous data handling.

15:04

πŸ› οΈ Advanced Features and Improvements in React 19

The final paragraph covers additional improvements in React 19, including the ability to pass refs as props to server components, the support for rendering document metadata directly within components, and the introduction of new APIs in `react-dom` for working with browser hints. Lydia also mentions the new support for style sheets in components and the automatic handling of resource loading priorities. The paragraph concludes with an overview of server actions, which are server-side functions callable from the client side, and their potential for offloading computations to the server. Lydia wraps up the talk by highlighting the overall enhancements in React 19 that simplify code and improve performance.

Mindmap

Keywords

πŸ’‘React 19

React 19 refers to the next major version of the React JavaScript library, which is a popular framework for building user interfaces. In the video, the presenter discusses new features and improvements in React 19, highlighting its impact on the development of responsive and efficient web applications.

πŸ’‘Transitions

Transitions, introduced in React 18, are a feature that allows for marking certain state updates as non-interruptible, ensuring the UI remains responsive. In the script, transitions are explained as a solution to prevent the UI from becoming unresponsive during complex state updates, which is vital for a smooth user experience.

πŸ’‘Start Transition

Start Transition is a function in React 19 that enables the passing of asynchronous functions to manage state updates in a way that maintains UI responsiveness. The script mentions that with React 19, developers can now wrap state updates within a 'start transition' to yield control back to the main thread periodically.

πŸ’‘Action

In the context of React 19, an action is any function that triggers a transition. The script explains that actions can be created using the 'useActionState' hook, which simplifies the process of handling state updates and provides status updates on the transition triggered by the action.

πŸ’‘Use Action State Hook

The 'useActionState' hook is a new feature in React 19 that facilitates the use of actions and provides status updates on transitions. It returns an array with the action's state, the action function itself, and a pending state indicator, streamlining the process of managing state and transitions.

πŸ’‘Server Components

React Server Components (RSC) allow React to send serialized componentry to the client in a special JSON-like format, enabling more efficient rendering without the need for HTML or JavaScript on the server side. The script discusses how RSC can access server-side functionality directly within components, revolutionizing the way applications are rendered and interacted with.

πŸ’‘Suspense Boundary

A Suspense boundary in React is used to wrap components that may have asynchronous data dependencies. In the script, it is mentioned that server components can be wrapped in a suspense boundary, allowing React to stream parts of the application to the client while rendering a fallback UI until the data is fetched and the components are ready to be displayed.

πŸ’‘Use API

The 'use' API in React 19 is not a hook but a new feature that works with promises or contexts, providing the resolved data or context value. The script explains that this API integrates with Suspense and error boundaries, simplifying the handling of asynchronous data on the client side.

πŸ’‘Use Optimistic Hook

The 'useOptimistic' hook allows developers to update the UI as if an action has already completed, before the actual data is returned. This can improve perceived performance by showing optimistic data to the user. The script provides an example where a message is shown to the user as if it has been sent, before the server confirms receipt.

πŸ’‘Use Client Bundle Directive

The 'use client bundle' directive is a feature in React 19 that adds components and their imports to the client-side JavaScript bundle, allowing them to be rendered on the client. The script mentions this directive as a way to explicitly include components in the client bundle for client-side rendering.

πŸ’‘Server Action

A server action in React 19 is a function that runs server-side but can be called from the client-side. The script describes server actions as a framework feature that simplifies working with server-side functionality within client components, offloading computations or data manipulations to the server.

Highlights

Introduction of React 19 with new stable features including the ability to pass asynchronous functions to start transitions.

Transitions in React 18 are now stable, providing a solution to keep the UI responsive during state updates.

React now yields back to the main thread every 5 milliseconds to handle user interactions during updates.

Use of transitions to batch and commit state updates for a single paint, improving user experience.

Refactor of state management with React 19 using the `useTransition` hook for less boilerplate code.

Introduction of the `useActionState` hook for easy management of action states and transitions.

React DOM now allows working with actions directly through the `action` prop for better form handling.

New `useFormStatus` hook for easier access to form submission status within child components.

Use of `useOptimistic` hook for updating the UI before a transition completes, providing immediate feedback.

Introduction of React Server Components for server-side rendering without needing to rebuild on the client side.

Server Components can access server-side functionality like databases and file systems directly within components.

Suspense boundaries can be used with Server Components to handle data fetching and rendering.

Use of `useClient` directive to add components to the client-side JavaScript bundle for interactive features.

New `use` API for handling promises and context in a more flexible way on the client side.

Server Actions allow server-side functions to be called from client-side components for offloading computations.

Server Components can now accept refs as props without the need for `forwardRef`.

Support for rendering document metadata directly within components in React 19.

Direct use of stylesheets in components with React 19, ensuring styles are loaded before component rendering.

Browser hints APIs in React 19 for preloading and prefetching resources to improve loading times.

Transcripts

play00:03

[Music]

play00:21

[Applause]

play00:23

good morning everyone or good day if

play00:26

you're watching this online I'm Lydia

play00:28

hiy and um I don't have time here so

play00:30

let's just get right into it what is new

play00:33

in react 19 now I know that many of you

play00:36

sitting here today have used you know

play00:37

Frameworks that already had Rea Canary

play00:39

bundled so not all of these features

play00:42

will be really new to many of you but

play00:44

there are many that are now stable in

play00:45

react 19 so the first one is that in

play00:48

react 19 we can now pass asynchronous

play00:51

functions to start transition so

play00:54

transitions were introduced in react 18

play00:57

as a way to Mark a certain State update

play00:59

as non

play01:00

and we can do this by wrapping this

play01:02

update in a star transition so by

play01:05

default react renders all the necessary

play01:07

components in a single uninterruptable

play01:10

task but depending on the complexity of

play01:13

this update it can take quite a while

play01:15

before reactors actually rendered these

play01:17

updates and then committed them to the

play01:19

Dom and since this is all happening on

play01:22

the main thread our or our application

play01:25

is unable to handle any other tasks

play01:28

during this time which leaves the

play01:30

unresponsive so if a user interacted

play01:32

with her website while react was

play01:34

rendering an update they would

play01:36

experience quite some visual feedback

play01:38

DeLay So transitions provide a solution

play01:41

to this because instead of rendering the

play01:43

update as a single uninterruptible task

play01:46

uh react will yield back to the main

play01:48

thread every 5 milliseconds to see if

play01:51

there are other tasks waiting to be

play01:53

handled instead for example that user

play01:55

interaction because if that's the case

play01:57

react will pause the current render let

play01:59

the browser handle the task and then

play02:01

eventually get back to rendering and it

play02:04

only commits these updates at the most

play02:06

performing time so transitions ensure

play02:08

that the UI stays responsive during an

play02:11

update now it's very common to have a

play02:13

scenario like the following where a user

play02:16

can submit some like input based on form

play02:18

data So within the submit Handler we

play02:20

first manually set it bending to true

play02:23

that causes react to rerender showing

play02:25

the pending States we then initiate the

play02:27

acing task eventually once that

play02:29

completes we set p into false and then

play02:30

handle the data or error that gets

play02:32

returned and this works but a few things

play02:36

could be better let's say that we first

play02:38

update the data with the name Lydia now

play02:41

if we rendered this to the UI saying

play02:42

like you successfully updated to the

play02:44

name Lydia or something um it could

play02:46

happen or the following like scenario

play02:48

could happen which looks a bit visually

play02:50

overwhelming but first we update the

play02:53

name to Lydia and while this submission

play02:55

is ongoing we change the name to Evan

play02:59

now finally some data gets returned

play03:01

because our Network request finishes so

play03:03

now we set the data in the UI so the

play03:06

state is now Lydia but the user might

play03:07

have expected Evan because that was the

play03:09

latest submission and that only happens

play03:12

after the second request return so this

play03:14

could lead to some like unpredictable

play03:17

suboptimal user experience and also

play03:19

there's just quite a lot of boilerplate

play03:21

to handle data pending and error States

play03:24

so in react 19 we can now refactor this

play03:27

to be a transition instead so the used

play03:30

transition hook provides both the is

play03:31

pending States and the start transition

play03:33

function so we no longer have to deal or

play03:36

manually set that pending state so now

play03:39

when the user submits some input the

play03:41

transition starts and it's pending is

play03:43

automatically set to true the data is

play03:45

fetched after which the transition then

play03:47

starts to update the state based on that

play03:49

returned data now a limitation at the

play03:52

moment is that by default State updates

play03:55

after and a weight keyword within start

play03:57

transitions are not marked as

play03:59

transitions by default so to make this a

play04:01

transition we do have to wrap it in yet

play04:03

another star transition uh this is just

play04:05

a limitation until Asing context is

play04:08

available so just something to keep in

play04:10

mind until then so now again if we have

play04:13

a similar situation as we had before so

play04:15

we first submit Lydia and then while

play04:17

that submission is ongoing we change it

play04:18

to Evan we only see one value render to

play04:22

the UI and this happens because if there

play04:24

are multiple ongoing transitions react

play04:26

will actually batch them and only commit

play04:28

the changes once all the transitions are

play04:30

done and in this case that only results

play04:33

in a single paint namely Evan so in R 19

play04:37

functions that trigger transitions are

play04:39

now called action so an action is just

play04:41

any function that triggers a transition

play04:43

which we can do in multiple ways the

play04:45

first one that we're now familiar with

play04:47

with star transition but we can also

play04:49

create an action using the new use

play04:51

action State hook and this is a new hook

play04:53

in R9 that makes it really easy to use

play04:55

actions and also get these status

play04:57

updates on that transition that this

play04:59

action triggered and also then set the

play05:01

state based on this return data so this

play05:03

hook returns an array with three values

play05:05

so first we have the action states which

play05:08

is either the data that we pass for the

play05:10

initial state so null in this case or

play05:12

it's the data that gets returned by the

play05:14

action once the action completed then we

play05:17

have the actual action um which returns

play05:19

that action function that we pass as the

play05:21

first argument and we can also or we can

play05:24

use this value to actually trigger that

play05:26

action to or invoke the action to

play05:28

trigger the transition

play05:30

and lastly we have the pending State

play05:32

indicator which is true as soon as the

play05:34

transition starts and then automatically

play05:36

set to false once the transition has

play05:38

been committed to the Dom so looking

play05:40

back at the code we had before even

play05:42

though this was already refactored with

play05:45

the asynchronous transition we still had

play05:46

to manually set that data so instead we

play05:50

can refactor this to use the use action

play05:52

State hook instead so we no longer have

play05:55

to have a separate data State because

play05:57

that is now just returned from the use

play05:59

action State Hook and the initial State

play06:01

value is now passed as the second

play06:03

argument which is null in this case the

play06:06

submit action is the first argument that

play06:08

we pass to the use action State hook

play06:11

which by default also receives the

play06:12

current state and the form data

play06:14

arguments and because we get access to

play06:16

this form data we also no longer have to

play06:19

manually set the state of the user's

play06:21

input instead we can just directly get

play06:23

this from form data and we can get the

play06:26

is pending state from the use action

play06:28

State hook as well

play06:30

so the behavior we see is the same just

play06:32

with a lot less boiler plates so

play06:34

invoking this action triggers a

play06:35

transition automatically sets a spending

play06:37

to true and then set a false once it's

play06:38

been

play06:39

committed now react Dom allows you to

play06:42

work with actions directly through the

play06:44

action prop or the form action prop for

play06:46

buttons and inputs so the values that we

play06:49

pass to action or the form action prop

play06:52

can be any function whether synchronous

play06:54

or asynchronous but they're all

play06:56

considered actions meaning they all

play06:58

trigger transitions

play07:00

so this action can either be from the

play07:02

use action State uh hook or just a

play07:04

standalone function and an action

play07:06

automatically receives the form data as

play07:08

a prop which makes it really easy to

play07:10

work with these form values and Rec 19

play07:13

also introduces a new use form status

play07:16

hook which makes it even easier to get

play07:18

the status information from the last

play07:20

form submission so this returns an

play07:23

object which includes the form data

play07:25

that's currently being submitted and

play07:27

also the transitions pending state

play07:30

so the use form status hook uses the

play07:32

action data from the parent form similar

play07:35

to how context work so we can use this

play07:37

hook within components that are rendered

play07:39

within forms and it will just directly

play07:41

access the values of the action so when

play07:44

the transition is pending pending is

play07:47

true by the or return True by the use

play07:49

form status hook and once the transition

play07:51

has been committed this is automatically

play07:53

set to false so creating reusable form

play07:55

elements is super easy this way and it's

play07:58

very easy to also provide that real time

play08:00

feedback and status updates to users and

play08:03

again it works pretty similar to context

play08:04

so just make sure that you don't use

play08:06

this hook directly with then the

play08:07

component that actually renders the form

play08:10

it has to be a child

play08:13

component now an asynchronous task

play08:15

within an action can take a while to

play08:17

actually return some data or maybe users

play08:19

are on slower internet connections

play08:21

Anything could happen and instead of

play08:23

showing a pending State we might want to

play08:25

show the data as if the action has

play08:28

already completed which we can do if we

play08:30

already know what the return data is

play08:31

going to be and for this we can use the

play08:33

new use act or use optimistic hook so

play08:37

the function returned from the use

play08:39

optimistic hook can be used within an

play08:41

action to update the UI before a

play08:43

transition completes and it receives a

play08:45

state which uh gets returned initially

play08:48

whenever no action is pending and also a

play08:50

function that we can then use to

play08:51

actually add that new optimistic state

play08:54

so the first element of the return array

play08:57

is the current state which is either the

play08:59

initial State when no action is pending

play09:01

or it's the returned value from the

play09:03

update function if an action is

play09:06

pending and the second value is the

play09:08

actual update function which we can use

play09:10

to actually you know invoke this

play09:11

function and set a new optimistic value

play09:14

so for example let's say that we have

play09:15

some messages that we receed from the

play09:17

server through the messages from server

play09:19

prop oh sorry no I have to go

play09:23

back so in the use optimistic hook we

play09:26

set the initial state to be the value of

play09:28

the messages from server prop so this is

play09:30

the initial value of the first returned

play09:33

element which in this case is called all

play09:35

messages so then we map over all

play09:37

messages and then render this which is

play09:39

just the initial messages from server

play09:42

now when a user submits a message the

play09:44

add message action is triggered and

play09:47

inside the add message action we first

play09:50

invoke the add optimistic message

play09:52

function of with the value of the

play09:54

message that we just typed and this

play09:56

triggers the update function of the use

play09:57

optimistic hook which then returns an

play09:59

array containing the current messages

play10:01

from the server along with this new

play10:03

optimistic

play10:05

message so this now updates the value of

play10:08

all messages because the pending

play10:09

transition is ongoing and because

play10:11

there's now changed value react will

play10:13

rerender and show the list of messages

play10:15

including that optimistic message and

play10:17

maybe to indicate to users that this is

play10:18

still like being sent we can display

play10:20

some sending or pending text stuff like

play10:22

that now we actually send the request

play10:25

and after some time the request succeeds

play10:27

and this updates the messages from

play10:29

server prop now since we use this value

play10:32

as the initial State all messages now

play10:34

gets this updated value which again

play10:36

causes a rerender so now we actually

play10:39

have all the messages but in the

play10:40

meantime the user saw the optimistic

play10:43

message so actions trigger transitions

play10:45

which are asynchronous by default but

play10:48

use optimistic essentially opts out of

play10:51

that asynchronous behavior so it's

play10:54

immediately reflected in the

play10:56

UI now moving on to some uh features Rec

play11:00

19 now also introduces Rec server

play11:02

components so traditionally we

play11:04

essentially had two ways to render our

play11:06

application so we could either use

play11:07

client side rendering in which case the

play11:09

server side react tree got rendered to a

play11:12

JavaScript bundle and then we had some

play11:13

minimal HTML that both got shipped to

play11:15

the clients where then the client site

play11:17

renderer uses this bundle to rebuild

play11:19

that react tree or we could use server

play11:21

side rendering in which case the react

play11:23

tree got the render to both HTML and a

play11:25

JavaScript bundle still sent to the

play11:27

clients and then this static HTML was

play11:30

made interactive through that bundle now

play11:32

both approaches rely on the fact that

play11:34

the react renderer needed to rebuild

play11:36

that reactory client side even though it

play11:39

already had it on the

play11:40

server so the introduction of the

play11:43

concurrent renderer in RCT 18 allowed

play11:45

for a different approach namely server

play11:47

or react server components so react

play11:49

server components allow react to send

play11:51

the actual serialized componentry to the

play11:53

client by using a special Json like

play11:56

format and the client that rer

play11:58

understands this format and can just

play11:59

rebuild that tree client site without

play12:01

the need for HTML or

play12:03

JavaScript so Rec server components are

play12:05

never sent to the client only their

play12:07

return value is in this Json like format

play12:10

so this means that we get to use server

play12:11

side functionality directly within our

play12:13

components like accessing databases the

play12:15

file system and so on and this is

play12:18

especially easy because in react 19 we

play12:19

can now use async a weight to render

play12:22

server components and the RSC payload

play12:24

Remains the Same because that's just a

play12:25

representation of what is actually

play12:28

returned now it can happen that this

play12:30

data lookup takes a while to complete

play12:33

and in that case the entire react tree

play12:35

needs to wait before it's sent to the

play12:37

client or before that data is completely

play12:38

fetched before it's sent to the client

play12:41

so to fix this we can wrap the server

play12:42

components in a suspense boundary and

play12:44

this allows re to already stream the

play12:46

parts that were faster to the clients

play12:48

and in the meantime render that suspense

play12:50

fall back only once the data has been

play12:52

fetched and the RC payload has been

play12:53

generated will this component actually

play12:55

be sent to the

play12:56

client now since re server components

play12:59

are are only rendered on the server and

play13:00

then rendered to Json without needing

play13:02

any javascripts we cannot use client

play13:04

side functionality by default and this

play13:06

includes things like hooks event

play13:08

handlers windows and so on so to render

play13:11

components client side we need to

play13:13

explicitly add them to the JavaScript

play13:15

bundle and to tell bundlers that we want

play13:17

to add them to the bundle including

play13:19

their Imports we can use the now stable

play13:21

use client bundler directive and adding

play13:24

this directive um adds the components

play13:26

and its Imports to the bundle that get

play13:28

chipped clients side so and react won't

play13:31

render this component on the server

play13:33

instead it takes the CSR the client site

play13:36

rendering approach

play13:38

here so now because we're actually

play13:40

shipping the component to the client and

play13:42

not just the return value we cannot

play13:44

fetch data the same way that we could

play13:46

with a server components but there are a

play13:48

couple things we can do here first we

play13:50

can just fetch the data within a server

play13:52

component and then pass this data as

play13:54

props to the client components or we can

play13:56

use the new use API within a client

play13:59

components and pass the promise as a

play14:01

prop instead of the actual data so use

play14:04

is a new API not a hook that accepts

play14:07

either a promise or a context and it

play14:10

Returns the data of the resolved promise

play14:12

in case we pass a promise or the value

play14:14

of the context in case we passed a

play14:17

context so in this case we're creating

play14:19

the uh reactions promise within the Sero

play14:22

components and instead of awaiting this

play14:25

value we're just passing the actual

play14:26

promise down to the client and on the

play14:29

client we then pass this promise to the

play14:31

use API so this API automatically

play14:34

integrates with suspense and error

play14:36

boundary so while we're waiting for that

play14:39

promise to resolve on the clients we can

play14:41

show these suspens is fall back and then

play14:43

once the promise resolves the fallback

play14:45

is switched to the actual components or

play14:47

in the case the prom is rejected the

play14:49

error boundary gets shown now the

play14:51

benefit of using the use API directly on

play14:53

the client is that the rest of the

play14:55

server components can already be

play14:57

streamed to the clients with without

play14:59

having to await that

play15:00

data so the use API makes it incredibly

play15:04

easy to work with asynchronous data

play15:06

client side whereas previously we had to

play15:08

deal with API endpoints and setting

play15:10

loading and error States react can now

play15:12

handle all of this for us which really

play15:14

simplifies our code now we can also use

play15:18

the use API with context which

play15:19

essentially replaces the use context

play15:22

hook so one big difference here though

play15:24

is that we can conditionally call it

play15:26

because it's a hook or it's an API it's

play15:28

not a hook so this means that we can

play15:29

call it after early returns within

play15:31

conditions and so on so the use API

play15:34

gives us a lot more flexibility to use

play15:36

context based on any runtime

play15:39

condition now in our server environment

play15:41

we can also create a server action and a

play15:44

server action is a function that runs

play15:46

server side but can be called client

play15:49

side and really this is more of a

play15:51

framework feature because it requires a

play15:53

framework to actually use it but we can

play15:55

create a server component uh within a

play15:57

component by using the use server

play15:59

directive so this can either be on top

play16:01

of a functions body that we then pass as

play16:03

a prop to the client components or we

play16:05

can create an entire file dedicated to

play16:08

just server actions by adding the use

play16:10

server directive on top of that file and

play16:12

then import the server action within the

play16:14

client

play16:15

component now it's up to the framework

play16:17

to actually implement this endpoint but

play16:20

whenever a server action is traed react

play16:22

will send the request to the server to

play16:24

execute this

play16:25

function and we can track the state of

play16:28

this request by getting more information

play16:30

about the action status for which we can

play16:32

use the use action State hook uh use

play16:34

start transition derive it from the use

play16:36

form status hook and so on so server

play16:38

actions make it incredibly easy to work

play16:41

with any server site functionality

play16:43

within client components because we no

play16:44

longer have to create those separate API

play16:47

endpoints and they're especially useful

play16:50

in scenarios whenever we want to offload

play16:52

certain computations or any data

play16:54

manipulations to the server which really

play16:56

reduces that load on the client

play16:59

now moving on from the server and client

play17:01

features some other improvements in

play17:03

react 19 so in react 19 we can now

play17:05

access ref as a prop for Server

play17:07

components so previously to pass a ref

play17:11

to a server component directly we had to

play17:13

use forward ref but starting with react

play17:15

19 function components now accept ref as

play17:18

a regular prop without the need for

play17:20

forward ref so just this change makes

play17:22

react feel a lot like closer to the

play17:24

platform because we no longer have to

play17:25

import anything from react actually M's

play17:27

work and also ref callbacks now allow a

play17:30

cleanup function for example to uh

play17:32

remove any event listeners which is

play17:34

really great to prevent any memory

play17:36

leaks re 19 add support for rendering

play17:40

document metadata text directly within

play17:42

components so whenever react renders

play17:44

this component it'll see the title link

play17:46

and meta tags and automatically hoist

play17:49

them to the head of the document so as

play17:51

previously we had to use tools like

play17:52

react helmet to handle this directly

play17:54

within components we can now just use it

play17:56

natively in react but for more more like

play17:59

custom metatags you might still want to

play18:01

use react

play18:02

helmet now react 19 also adds support

play18:05

for directly using style sheets in the

play18:08

component that actually uses these

play18:09

Styles so we can just directly add them

play18:11

to our components and react will again

play18:13

hoist these to the head which ensures

play18:15

that the browser will not render the

play18:16

components until the CSS has loaded and

play18:19

we can also load a stylesheet within a

play18:21

suspense boundary so in this case we're

play18:24

loading the uh button and the common CSS

play18:26

within the suspense boundary and since

play18:28

the CSS has a higher precedence because

play18:30

that's high react ensures that this

play18:33

Styles sheet will be loaded first and

play18:35

when loading CSS Styles in a suspense

play18:37

boundary react will keep rendering the

play18:40

suspense fallback until the sty sheet is

play18:42

inserted into the head and fully loaded

play18:45

it will only reveal the content once the

play18:46

CSS is completely loaded so this

play18:49

prevents that style of or that flash of

play18:51

unstyled

play18:53

content now lastly staying in the head

play18:55

components uh react 19 also includes new

play18:58

a apis within react Dom for working with

play19:01

browser hints So within our components

play19:03

we can use apis like preload preit

play19:06

prefetch DNS and many more to use these

play19:08

browser hints and browser hits can be

play19:10

really useful to fetch or connect to

play19:12

certain resources before they are

play19:14

discovered which can really help to

play19:16

speed up certain loading times and react

play19:19

handles the hoisting to the head but

play19:21

also automatically reorders the browser

play19:23

hints based on their priority to early

play19:25

loading so as developers we don't have

play19:27

to worry about the orders like uh

play19:30

performance implications so this ensures

play19:33

that the most critical resources are

play19:35

made available as quickly as

play19:37

possible now in the next talk Sam sakov

play19:40

will show you how to use these new

play19:42

features by refactoring some existing

play19:45

react code with these new apis thank you

play19:47

so much for coming to my talk and have

play19:49

fun building with react 19

play19:57

[Music]

play19:58

[Applause]

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
React 19UI TransitionsServer ComponentsConcurrencyHooksSuspenseOptimistic UIClient-Server InteractionWeb DevelopmentJavaScript