React 18 was out on March 2022 with some great new features and a focus on performance improvements.
In this tutorial, we will discuss about react 18 features and updates, we will also discuss few important concepts like concurrent rendering, automatic batching, transition, and suspense on the server.
Related read: Migrating to the Latest React Version
CATEGORY | NEW FEATURE |
Concept | Concurrent Rendering |
Features | Automatic Batching, Transitions, Suspense on the server |
APIs | createRoot, hydrateRoot, renderToPipeableStream, renderToReadableStream |
Hooks | useId, useTransition, useDeferredValue, useSyncExternalStore, useInsertionEffect |
Updates | Strict Mode |
Deprecated | ReactDOM.render, renderToString |
Let’s deep dive into React 18 new features and learn more about it.
This is for you if your project is still in React 17 and wants to upgrade to React 18. So you need to follow some steps for it, and they mention below.
Install React18 and ReactDOM by using npm or yarn like
npm install react react-dom
else,
yarn add react react-dom
After successfully installing it, you must change the code in the index.js file. Without these changes, the application will throw some warnings because React 18 changed ReactDOM.
What’s It Look Like In React 17 ?
import ReactDOM from "react-dom"; import App from "App"; ReactDOM.render(<App />, document.getElementById("root"));
And How It’s Look Like In React 18
import ReactDOM from "react-dom"; import App from "App"; const rootElement = document.getElementById("root"); const root = ReactDOM.createRoot(rootElement); root.render(<App />);
It is enough for running our application on React 18. Now moving forward to react 18 new features and looking at them individually.
Here is a list of a few updates.
This new update is very important for the performance of our application. Concurrent rendering means rendering those things first, which is more important than others.
Let’s take an example for better understanding.
Suppose we need to talk with two people – person 1 and person 2. Before React 18, in a non-concurrent setting, we can have only one call at a time, and after the first call, we can have another call.
Like if we are talking with a person 1, after cutting this call, we can call Person 2. This is fine when calls are sorted but think about if calls are long. This can be a time sink.
In a contemporary setting, we could call Person 1, and once we were put on hold, we could call Person 2.
It does not mean that we are talking simultaneously, but it just means that we have more than one call simultaneously and can decide which ring is more important.
Similarly, React 18 came up with concurrent rendering; it can interrupt, pause, resume, or abandon rendering. This allows us to react to render those things first, which has more priority and respond to the user interaction quickly, even if it is on a heavily loaded task.
Before This update, React 17 was rendering single, uninterrupted, and synchronized, and once rendering started, it could not be interrupted.
Automatic batching is one of the best features that contributes more to making applications faster. Let’s go through an example and understand what automatic batching is.
Suppose you want to make a pizza. So you plan to go to the market and buy all ingredients which you need for pizza. So you make the trip, buy all the ingredients at once, and come back with all the elements.
This is batching. Without batching, you would start cooking pizza and find out what ingredients you need at a time. Like it would be best if you had salt, so go to the market, purchase it, come back and again start cooking. Then again, it would help if you had some other ingredient. So again, go to the market, buy it, come back, start cooking, and drive yourself crazy.
In React 18, the automatic batching reducer re-rending and make the application faster.
Let’s see the code
import React,{useState} from 'react' const App=()=>{ const [name,setName]=useState(""); const [email,setEmail]=useState(""); const [phone,setPhone]=useState(0); const updateData=()=>{ setName("person"); setEmail("react@gamil.com"); setPhone(111111111); } console.warn("update state") return( <div> <h1>React 18</h1> <button onClick={updateData}>Update all data</button> </div> ) }
If we run this code, we can see only a one-time “update state” in the console, which is the beauty of automatic batching. For example, suppose we have 10 state updates in the update data function, and when the user clicks on the update button, it will re-render only once and update all the states.
That is how Automatic batching works.
If we want to re-render our application after the updation of the state, ReactDOM gives us the callback function flushSync. Let’s see the code.
const updateData=()=>{ flushSync(()=>{ setName("person"); }) flushSync(()=>{ setEmail("react@gmail.com"); }) flushSync(()=>{ setPhone(111111111); }) }
Now it will re-render the code after each update of state.
This feature is used when one state depends on another. For example, when we search something like open your browser and write “test” into the searchBar, what happens there is the first set state of test which we wrote into the searchBar.
We see the result related to what we wrote into the searchBar, so basically, update the first state and then find the development related to the first state, store it in the second state, and show it below the search bar.
Here, the user typing in the search bar is more important than showing the result. Searching is not urgent and so can be non-urgent.
startTransition is a callback function, and We can mark our task as non-urgent by using startTransition.
Import { startTransition } from 'react'; // Urgent : Show what was typed setInputValue(input) // Mark any non-urgent state updates inside as transitions startTransition(()=>{ setSearchResult(input) })
Suspense is a very interesting feature that has been improved in react 18. Let’s see via one example.
To make our application interactive, JavaScript plays a big role. For example, suppose you have a poor connection, or your JavaScript bundle is huge. In this case, the process can take a long time, or the user must wait for the page to become interactive or see meaningful content.
One component has a huge JavaScript bundle, so our application became slower by only this component. One slow feature can slow down our entire page.
Now React 18 adds support for the suspense on the server. With the help of the suspense will load out our other component, which is faster and shows a loading or placeholder for the slower component that will not get the real data.
This technique is very easy to use. Just wrap your the slow part of your application. See the below code.
<Suspense fallback={<Spinner />}> <ProfilePage /> </Suspense>
Strict Mode is a tool for highlighting some problems in an application. For example, if we forget to add a key to a list or something else, Strict Mode warns about it in the console. Furthermore, it activates additional checks and warnings for its descendants.
Strict mode checks in only development mode. It does not impact the production build.
To enable strict mode for the application, see the below code. Then, open index.js and wrap the App component with React.StrictMode.
root.render( <React.StrictMode> <App /> </React.StrictMode> );
React abandoned support for Internet Explorer in this release due to modifications and the introduction of React 18 new features. The latest update is built on modern browser features such as microtasks which cannot be adequately polyfiled in IE.
Related Read : React Native Form Validation Using Formik And Yup
In this article, we have covered React 18 features and how they work, and their functionality.
The team at Mindbowser was highly professional, patient, and collaborative throughout our engagement. They struck the right balance between offering guidance and taking direction, which made the development process smooth. Although our project wasn’t related to healthcare, we clearly benefited...
Founder, Texas Ranch Security
Mindbowser played a crucial role in helping us bring everything together into a unified, cohesive product. Their commitment to industry-standard coding practices made an enormous difference, allowing developers to seamlessly transition in and out of the project without any confusion....
CEO, MarketsAI
I'm thrilled to be partnering with Mindbowser on our journey with TravelRite. The collaboration has been exceptional, and I’m truly grateful for the dedication and expertise the team has brought to the development process. Their commitment to our mission is...
Founder & CEO, TravelRite
The Mindbowser team's professionalism consistently impressed me. Their commitment to quality shone through in every aspect of the project. They truly went the extra mile, ensuring they understood our needs perfectly and were always willing to invest the time to...
CTO, New Day Therapeutics
I collaborated with Mindbowser for several years on a complex SaaS platform project. They took over a partially completed project and successfully transformed it into a fully functional and robust platform. Throughout the entire process, the quality of their work...
President, E.B. Carlson
Mindbowser and team are professional, talented and very responsive. They got us through a challenging situation with our IOT product successfully. They will be our go to dev team going forward.
Founder, Cascada
Amazing team to work with. Very responsive and very skilled in both front and backend engineering. Looking forward to our next project together.
Co-Founder, Emerge
The team is great to work with. Very professional, on task, and efficient.
Founder, PeriopMD
I can not express enough how pleased we are with the whole team. From the first call and meeting, they took our vision and ran with it. Communication was easy and everyone was flexible to our schedule. I’m excited to...
Founder, Seeke
We had very close go live timeline and Mindbowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
Mindbowser was great; they listened to us a lot and helped us hone in on the actual idea of the app. They had put together fantastic wireframes for us.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork