NEW React 19 Changes Are Amazing!

Web Dev Simplified
27 Feb 202408:42

Summary

TLDRIn this video, Kyle from Web Simplified breaks down the key updates in React version 19, aiming to simplify the transition for developers. The most significant change is the introduction of a compiler, which will automatically handle memorization, reducing the need for hooks like `useMemo`, `useCallback`, and the `memo` function. This simplifies code and enhances performance. React 19 also stabilizes features like actions and directives for client and server-side rendering, making form handling more intuitive with new hooks like `useFormStatus` and `useFormState`. Additionally, it introduces `useOptimistic` for optimistic UI updates and improves suspense for loading styles and scripts. The update promises better integration with web components and eliminates the need for `forwardRef` and `react.lazy` with the new `use` hook. This comprehensive overview highlights React's commitment to making development more accessible and efficient.

Takeaways

  • 🚀 **React Compiler Introduction**: React 19 will include a compiler, which is a significant change as React historically ran in the browser without a compile step. This compiler will automatically handle memorization, reducing the need for manual hooks like `useMemo`, `useCallback`, and `memo`.
  • 📝 **Simplified Code and Performance**: The compiler will simplify code by removing the need for specific hooks, making it easier to read and write, and likely improving performance by identifying more opportunities for memoization than developers might find themselves.
  • 🔄 **Actions and Directives**: React 19 will stabilize and expand the use of actions and the `use client`/`use server` directives, allowing for server-side execution of functions that were previously limited to client-side, enhancing flexibility and performance.
  • ⚙️ **Form Handling Enhancements**: New hooks `useFormStatus` and `useFormState` will streamline form handling, reducing boilerplate code for managing form states, errors, and loading states.
  • 🌟 **Optimistic Updates**: The `useOptimistic` hook will facilitate optimistic UI updates, providing a seamless user experience by showing immediate feedback (e.g., a 'like') before server confirmation.
  • 📜 **Client and Server Directives**: The `use client` and `use server` directives will become stable features, enabling frameworks other than Next.js to leverage these capabilities for better server-side rendering and client-side execution control.
  • 📄 **Document Metadata**: React 19 will improve handling of metadata within components, allowing for tags like `title`, `meta`, and `link` to be placed anywhere in React components and automatically positioned correctly in the document head.
  • 🔗 **Suspense for Styles and Scripts**: Suspense, introduced in React 18, will be enhanced to ensure that styles, links, and scripts are loaded and executed before the page content is displayed, improving the consistency of the rendering process.
  • 🌐 **Improved Web Components Support**: React 19 will offer better integration with web components, making it easier to use them within React applications without requiring separate React-specific versions.
  • 🔄 **No Need for `forwardRef`**: The `forwardRef` function will become obsolete as React 19 will automatically pass the `ref` prop to every component, simplifying the process of handling references.
  • 🆕 **New `use` Hook**: The addition of the `use` hook in React 19 will allow for asynchronous loading of JavaScript files, promises, and context, providing a powerful but complex tool for advanced use cases.

Q & A

  • What is the biggest change coming in React version 19?

    -The biggest change is the addition of a compiler to React. This is significant because previously React only ran in the browser without a compile step, but with the new compiler, React will automatically handle optimizations such as memorization that were previously manual through hooks like useMemo, useCallback, and the memo function.

  • Why is the introduction of the React compiler considered a win for developers?

    -The introduction of the React compiler is a win because it simplifies code by eliminating the need for manual performance optimization hooks, making the code easier to read and write. Additionally, it is expected to make code faster as the compiler is likely to identify more opportunities for memoization than developers would manually.

  • What are 'actions' in the context of React and Next.js?

    -Actions in React and Next.js are functions that can handle form submissions. They allow for the processing of form data, and with React 19, these actions will become a stable feature that can run on either the client or the server, providing a more built-in way to deal with forms, including submissions, error states, loading, and data management.

  • How do the new hooks useFormStatus and useFormState simplify form handling in React?

    -The useFormStatus and useFormState hooks simplify form handling by automating the management of form state and loading status. This reduces the amount of boilerplate code needed to handle form submissions, errors, and loading messages, making the process more straightforward and easier to implement.

  • What is the purpose of the useOptimistic hook?

    -The useOptimistic hook allows for optimistic updates in React applications. This means that it can immediately reflect changes, such as a 'like' action, to the user interface before the server has confirmed the action, providing a smoother and more immediate feedback to user interactions.

  • What is changing with the use client and use server directives in React 19?

    -In React 19, the use client and use server directives are becoming a stable feature. This means that they will no longer be experimental and can be used to determine if code runs on the server or the client. This change is expected to enable other frameworks besides Next.js to adopt these directives.

  • How does the document metadata feature in React 19 help with server-rendering?

    -The document metadata feature allows developers to place title tags, meta tags, and link tags within React components, which are then automatically moved to the correct location in the HTML head. This ensures that metadata is properly managed, which is particularly beneficial for server-rendered pages and React server components.

  • What is the role of the Suspense feature in React 18, and how is it being enhanced in React 19?

    -Suspense is a feature in React 18 that allows for the streaming of data to be rendered. In React 19, it is being enhanced to ensure that it waits for style tags, links, and scripts to load before displaying the page. This helps prevent scenarios where a page renders without the necessary CSS or scripts loaded.

  • How will React 19 improve the integration of Web Components?

    -React 19 will improve the integration of Web Components by making them work much better with React. This will simplify the process of implementing Web Components within a React project, addressing past difficulties and eliminating the need for separate Web Component and React-specific versions of libraries.

  • What is the impact of the change to the ref prop in React 19?

    -In React 19, the ref prop will be automatically passed to every component, eliminating the need for the forwardRef function. This simplifies the process of using refs within React components.

  • What is the use hook in React 19, and how does it differ from other hooks?

    -The use hook in React 19 is a new addition that allows for the asynchronous loading of JavaScript files, running promises, and managing context in a more complex and flexible way than traditional hooks. It breaks the rules of normal hooks by allowing asynchronous operations and is considered one of the most powerful but potentially complex hooks added to React.

Outlines

00:00

🚀 Introduction to React 19's Major Updates

This paragraph introduces the upcoming changes in React 19, emphasizing the addition of a compiler which will automatically handle performance optimizations previously done manually with hooks like `useMemo`, `useCallback`, and the `memo` function. The compiler aims to simplify code and improve performance. Additionally, it covers the incorporation of 'actions' for form handling, similar to Next.js, and the expansion of server and client directives for more than just Next.js. The paragraph also mentions new hooks like `useFormStatus`, `useFormState`, and `useOptimistic` for handling form submissions, state, and optimistic updates, respectively.

05:01

📄 Enhancing React with Metadata, Suspense, and More

The second paragraph discusses additional features in React 19. It covers the ability to place metadata tags like titles, meta, and link directly in React components, which will be correctly positioned in the HTML head. This feature is particularly useful for server-rendered components. The paragraph also explains improvements to the Suspense feature from React 18, ensuring that styles and scripts are loaded before the page content, preventing CSS from being missing during initial render. Furthermore, it touches on better support for web components in React 19, making it easier to integrate them into React projects. Lastly, it mentions the deprecation of `forwardRef` and the introduction of the `use` hook for asynchronous loading and running of JavaScript files, promises, and context.

Mindmap

Keywords

💡React Compiler

The React Compiler is a new feature in React version 19 that automates the optimization of React applications. It is significant because, traditionally, React did not include a compile step and relied on developers to use hooks like `useMemo`, `useCallback`, and the `memo` function for performance optimizations. The compiler will handle these optimizations automatically, reducing the need for manual code and potentially improving performance by identifying more opportunities for optimization. This simplifies the code and makes it more efficient, which is a major focus of the video.

💡Actions

Actions in React 19, which are also a feature in Next.js, allow developers to handle form submissions in a more streamlined way. Instead of using the traditional `onSubmit` handler, an action function can be passed that takes form data and performs operations, which can even be server-side with Next.js. This feature is being stabilized in React 19 and is expected to work within both client and server applications, making form handling more intuitive and reducing boilerplate code for state and loading management.

💡useClient and useServer Directives

The `useClient` and `useServer` directives are used in Next.js to determine where certain code should execute—either on the client or the server. With React 19, these directives are becoming a stable feature, which means they can be used more broadly across frameworks beyond Next.js. This stability allows for better control over code execution context and is a step towards more innovation and interoperability between different frameworks.

💡Document Metadata

Document Metadata in React 19 allows developers to define title, meta, and link tags directly within React components. These tags are then automatically placed in the correct location in the HTML head by React. This feature is particularly useful for server-rendered applications and improves the handling of metadata, which was previously challenging with libraries like React Helmet.

💡Suspense

React Suspense, introduced in React 18, is a feature that enables the deferred loading of components. React 19 enhances Suspense by ensuring that it waits for stylesheets, scripts, and other resources to load before rendering the associated components. This helps to prevent scenarios where a page renders without the necessary CSS or JavaScript, thus improving the user experience and ensuring a more consistent rendering process.

💡Web Components

Web Components are a set of technologies that allow for the creation of custom, reusable, encapsulated HTML tags. In the script, it is mentioned that React 19 will improve support for Web Components, making it easier to integrate them into React applications. Previously, using Web Components with React required workarounds or specific libraries due to compatibility issues, but the improved integration will simplify this process.

💡useMemo and useCallback Hooks

The `useMemo` and `useCallback` hooks in React are used to optimize performance by memoizing values and callbacks, respectively. With React 19's compiler, these hooks will be less necessary as the compiler will automatically handle memorization, reducing the complexity of the code and the cognitive load on developers.

💡Memo Function

The `memo` function in React is a higher-order component used to optimize the rendering of components by memoizing the result and only re-rendering when props change. The introduction of the React Compiler in version 19 will automate this optimization, making the manual use of `memo` less relevant.

💡useFormStatus and useFormState Hooks

These are new hooks in React 19 that aim to simplify form management within React applications. `useFormStatus` and `useFormState` will handle aspects like form data, loading, and error states, reducing the amount of boilerplate code needed and making it easier for developers to work with forms.

💡useOptimistic Hook

The `useOptimistic` hook allows for optimistic UI updates, where the interface immediately reflects a user action, such as 'liking' a post, before the server has confirmed the action. This creates a smoother user experience by showing the expected result instantly and then correcting it if there is an error, which is a powerful concept for applications that require a responsive UI.

💡use Hook

The `use` hook in React 19 is a new addition that enables asynchronous loading of various resources, including JavaScript files, promises, and context. It is a complex hook that deviates from the typical rules of hooks in React, offering a wide range of possibilities for loading and managing asynchronous operations within components.

💡Forward Ref

In React, the `forwardRef` function is used to forward a `ref` attribute to a child component. According to the script, React 19 will handle this automatically, eliminating the need for developers to use `forwardRef`. This change will simplify the process of using refs within React components.

Highlights

React version 19 will include a compiler, which is a major change as React traditionally ran only in the browser without a compile step.

The compiler will automatically handle memorization, reducing the need for manual performance optimizations like `useMemo`, `useCallback`, and the `memo` function.

The introduction of the compiler will simplify code and improve performance by automatically finding instances for memoization more effectively than manual methods.

Actions, a feature from Next.js, will become a stable feature in React 19, allowing form data to be processed either on the client or server.

New hooks `useFormStatus` and `useFormState` will facilitate easier handling of form submissions, error states, and loading data.

The `useOptimistic` hook will enable optimistic updates, showing immediate feedback to user actions before server confirmation.

Client and Server directives (`use client` and `use server`) will become stable, allowing other frameworks to utilize these features beyond Next.js.

Document metadata can now be placed within React components, and React will ensure it is correctly positioned in the HTML.

Suspense, a feature from React 18, will be empowered in React 19 to wait for style tags, links, and scripts to load before rendering.

Web components will work better with React 19, making it easier to implement them in React projects.

The `forwardRef` will no longer be necessary as the `ref` prop will be automatically passed to every component in React.

The `use` hook will be added to React, allowing for asynchronous loading of JavaScript files, promises, and context.

The `use` hook is complex and breaks the rules of normal hooks, offering new possibilities for React development.

React 19 will make applications faster and easier to write by streamlining performance optimizations and form handling.

The new features in React 19 aim to reduce boilerplate code and improve the developer experience with built-in utilities.

Kyle, the presenter, offers a free course to keep developers updated on new React hooks as they are released.

The free course covers all the new hooks in React, including in-depth explanations and practical applications.

Transcripts

play00:00

staying up to dat with everything that's

play00:02

changing in react is incredibly

play00:03

difficult so in this video I'm going to

play00:04

go through the seven main things that

play00:06

you need to understand about react

play00:08

version 19 I'm going to make it as

play00:09

simple and quick as possible so you can

play00:11

get up to speed with everything you need

play00:12

to know before it even

play00:17

releases welcome back to webd simplified

play00:19

my name is Kyle and my job is to

play00:21

simplify the web for you so you can

play00:22

start building your dream project sooner

play00:24

as you can see there's a lot of

play00:25

documentation that react has released

play00:27

about what's going to be happening in

play00:28

version 19 I'm going to go through in

play00:30

order of importance everything that's

play00:31

changing and the biggest change is going

play00:33

to be that they're actually adding a

play00:35

compiler to react this is actually a

play00:37

really big deal because For the Longest

play00:39

Time react only ran in the browser and

play00:40

there's no compile Step at all and now

play00:42

other Frameworks have come along such as

play00:44

Astro and spelt and they added in their

play00:46

own compile step and this compile step

play00:48

just takes care of a lot of things for

play00:50

you behind the scenes so you don't have

play00:51

to write extra code for that but since

play00:53

react never had that we had different

play00:55

hooks like use memo use callback and the

play00:57

memo function that allowed you to do

play00:59

extra performance things to make sure

play01:00

your code ran performant since there was

play01:02

no compiler to do that for you

play01:04

automatically well with react version 19

play01:06

the big thing that they're pushing is

play01:07

this react compiler so that means that

play01:09

automatically react is going to add its

play01:11

own memorization for things like use

play01:13

memo use callback and the memo function

play01:15

so you never have to use those hooks or

play01:17

functions ever again it's just going to

play01:19

be automatically taken care of for you

play01:21

by the compiler so anytime you would

play01:22

normally need to use memo or use call

play01:24

back you can just completely remove that

play01:26

and the compiler will take care of it

play01:27

for you now this is a huge win because

play01:29

not not only does it make your code

play01:31

simpler and easier to read and write

play01:32

since you no longer have to think about

play01:34

this but it also will make your code

play01:36

faster because it's almost always the

play01:37

case that the actual compiler is going

play01:39

to find more instances of where you

play01:41

should be doing memoization than where

play01:43

you would normally find it yourself so

play01:45

having this extra compile step is going

play01:47

to make your application faster and

play01:48

easier to write and is honestly the

play01:50

thing I'm most excited about for react

play01:51

version 19 because these hooks are the

play01:54

reason that react is so confusing and

play01:56

complicated to write especially as you

play01:57

get larger and larger applications where

play02:00

performance is really important now the

play02:02

next two main features I want to talk

play02:03

about are something that you may be

play02:04

familiar with if you've already worked

play02:06

inside of nextjs and that's the idea of

play02:08

actions as well as the used client and

play02:10

the used server directive we'll start by

play02:12

talking about actions since this affects

play02:14

you a little bit more directly but if

play02:16

you've ever worked inside a nextjs you

play02:17

know that you can create a form and

play02:19

instead of passing an on submit you pass

play02:20

it an action and this action will take

play02:22

in all of your different form data and

play02:24

you can do whatever you want with this

play02:25

action and if you're using nextjs this

play02:28

action can even run on the server in

play02:29

instead of running in the client well

play02:31

the big change with react 19 is first

play02:33

that they're making that this is a

play02:34

stable feature because right now it's

play02:36

not really a stable feature and secondly

play02:38

they're adding it so it actually works

play02:40

inside of client or server applications

play02:42

so this action that I pass in here which

play02:44

is just a function that takes in my form

play02:46

data can either run on the client or it

play02:48

can run on the server and it has other

play02:50

hooks built around it that make dealing

play02:52

with State and loading and so on

play02:54

incredibly easy as you can see here we

play02:55

have hooks called use form status and

play02:57

use form State and if you want to learn

play02:59

more about these new hooks I actually

play03:01

have an entirely free course on every

play03:03

single react hook you need to know and I

play03:05

constantly update this course whatever

play03:07

new hooks are released so if that sounds

play03:08

interesting check out the link down in

play03:10

the description below and sign up for

play03:11

that course and again I'll update it as

play03:13

soon as new hooks are released or

play03:14

changed but essentially this new way of

play03:16

using an action instead of an onsubmit

play03:18

just gives you a more built-in way in

play03:20

react to actually deal with forms form

play03:22

submissions form error states form

play03:24

loading form data everything that there

play03:26

is to do with forms can be done much

play03:28

easier by using this action instead of

play03:30

using an onsubmit Handler cuz a lot of

play03:32

that boiler plate like loading error

play03:33

messages and so on is automatically

play03:35

taken care of for you by these two hooks

play03:37

of use form status and use form State

play03:40

and now another additional hook that's

play03:41

being added is use optimistic again I

play03:43

entirely cover that hook inside of this

play03:45

course and this hook just allows you to

play03:47

do optimistic updates so for example if

play03:49

you click on the like button on like a

play03:51

tweet or a post what will happen is

play03:53

it'll automatically show you that that

play03:54

has been liked while the server is

play03:56

processing and then once the actual

play03:58

result comes back it'll either change it

play04:00

to liked or unliked based on if there's

play04:01

an error or not so an optimistic update

play04:03

just shows you the most optimistic thing

play04:05

like saying okay if you click on this

play04:07

like button you want to like it so it'll

play04:08

show is liked immediately even if it

play04:10

hasn't been submitted to the server yet

play04:12

so this is just a really simple hook

play04:13

that makes doing that much easier and

play04:15

it's honestly really cool and magical

play04:16

how this actually works and again I have

play04:18

a full video on that in the course and

play04:20

this hooks course that I have is also

play04:22

completely free by the way I'm not sure

play04:23

if I mentioned that so if you are

play04:24

interested in it's completely free all

play04:26

you have to do is sign up for the course

play04:27

nothing else now the next thing is

play04:29

talking about the actual client versus

play04:31

server directives so you know that you

play04:32

can put use client or use server at the

play04:34

top of a file inside of nextjs to

play04:36

determine if that code is running on the

play04:37

server or if that code is being pushed

play04:39

down and ran on the client so this is

play04:41

just a really easy way for you to

play04:43

distinguish between these two things and

play04:44

right now it's really only usable inside

play04:46

of nextjs but with this newest version

play04:48

of react 19 that's coming out this is

play04:50

going to be a stable release instead of

play04:52

something that is more of an

play04:53

experimental status which means other

play04:55

Frameworks can be built around these

play04:56

used client and used server features and

play04:58

we will hopefully see more Frameworks

play05:00

than just nextjs or remix actually have

play05:03

these used client and used server

play05:04

features and we can really start to play

play05:06

around with the power of this in more

play05:07

than just nextjs which I think is really

play05:09

cool and important because more and more

play05:11

Frameworks add to more Innovation which

play05:13

pushes all the Frameworks further now

play05:15

the next thing I want to talk about is

play05:16

this document metadata section and

play05:17

essentially what this allows you to do

play05:19

is to put a title tag a meta tag or a

play05:22

link tag that deals with metadata

play05:23

anywhere inside of your react component

play05:26

and it's automatically going to make

play05:27

sure it moves it to the right location

play05:28

inside of like the or wherever else it

play05:30

needs to go so if you want to have like

play05:32

a different title on multiple different

play05:33

pages you can just drop this title

play05:35

component anywhere in your component and

play05:37

it's just going to work out of the box

play05:38

which is really nice and they mostly did

play05:40

this so that you can have certain things

play05:42

work fine when you're doing server

play05:43

rendering and react server components

play05:45

they did this because certain things

play05:46

like react helmet and other libraries

play05:48

like that won't work super well with

play05:49

this new way of doing things in react so

play05:51

they just built in their own way of

play05:52

doing that which is just a nice quality

play05:54

of life feature the next thing I want to

play05:56

talk about has to do with suspense so

play05:58

you may not be familiar with suspense

play05:59

it's a new react 18 feature that allows

play06:01

you to essentially stream in things so

play06:03

you can stream your data in to be

play06:05

rendered it's really popular in

play06:06

something like nextjs and essentially

play06:09

what they're doing here is empowering

play06:10

suspense by making sure that it waits

play06:12

for certain things like your style tags

play06:13

links and scripts to load so if you have

play06:15

certain style tags that are only loaded

play06:17

on certain pages or certain components

play06:19

or you have script tags that are running

play06:20

on only certain pages this is going to

play06:22

make sure that all your Styles and all

play06:24

your script tags are actually downloaded

play06:26

and ran before the page actually shows

play06:28

that way you don't have an instance

play06:29

where your page renders but none of your

play06:31

CSS has been downloaded yet this is just

play06:33

essentially making sure all of that will

play06:34

work well together and it even gives you

play06:36

a simple API that you can use to do this

play06:38

with more than just style link and

play06:40

script tags if you really want to but

play06:41

that's a rather Advanced feature now

play06:43

those are the main features mentioned in

play06:45

this article but there's actually quite

play06:46

a few additional things that are coming

play06:47

along one thing is that web components

play06:49

are going to work much better in react

play06:51

19 they mention it very briefly inside

play06:53

this article it's like one sentence that

play06:55

they mention it but they do mention that

play06:57

web components are now going to be

play06:58

working much better with react 19 so if

play07:00

you want to use web components in react

play07:02

it should be much easier to implement

play07:04

them inside of a react project because

play07:05

in the past that was something that was

play07:07

really difficult which is why you would

play07:08

almost always see when you're working at

play07:10

libraries there would be a web component

play07:11

library and then like a react specific

play07:13

version because it was so hard to make

play07:15

web components work in react but now it

play07:16

should be much easier to get them to

play07:18

play well together now these last couple

play07:20

features are really small so I'll go

play07:21

through them quite quickly the first is

play07:22

that forward ref is something you are

play07:24

never going to need to use ever again

play07:26

and that's because the ref prop that you

play07:28

normally get by using forward ref is

play07:30

automatically going to be passed to

play07:31

every component inside of react so you

play07:33

can just use refs by just passing it

play07:35

along as a second prop and you don't

play07:36

need this crappy forward ref function

play07:38

wrapping stuff anymore after this next

play07:41

is that you actually won't really need

play07:42

to use react. La anymore and that's

play07:44

because a brand new hook called use just

play07:46

simply use is being added to react and

play07:49

this use hook allows you to do a lot of

play07:50

really cool stuff it allows you to

play07:52

essentially asynchronously load things

play07:54

whether that's going to be

play07:54

asynchronously loading JavaScript files

play07:57

whether it's going to be asynchronously

play07:58

running different proc promises or maybe

play08:00

you even want to run your context

play08:02

through here all of those things are

play08:03

possible with the use hook it's honestly

play08:05

a rather complex hook that breaks all of

play08:07

the rules of normal hooks so if you want

play08:08

to learn more about that hook I highly

play08:10

recommend checking out that free course

play08:12

I go in- depth into everything you can

play08:13

do with this hook because honestly it's

play08:15

one of the coolest hooks that was added

play08:16

to react but it can be incredibly

play08:18

complicated because like I said it

play08:19

breaks all of the rules of every other

play08:21

hook out there so if you're interested

play08:23

in checking out this use hook I highly

play08:24

recommend checking out that free course

play08:26

and that's all the big new changes

play08:28

coming to react 19 if you want to make

play08:30

sure you stay up to date with all the

play08:31

new hooks that are coming out in react

play08:32

again I highly recommend you check out

play08:34

that free course because as soon as new

play08:35

hooks are added or changed I constantly

play08:37

update that course with that said thank

play08:39

you very much for watching and have a

play08:40

good day

Rate This

5.0 / 5 (0 votes)

Related Tags
React UpdatesWeb DevelopmentCompiler IntegrationPerformance OptimizationHooks SimplifiedForm HandlingServer-Client DirectivesMetadata ManagementSuspense APIWeb ComponentsAsync LoadingReact 19 Features