Home » What New in React 18

What New in React 18

react18

With the release of the latest version of React, Yarn will be its default package manager. This means faster installation, no more dependency issues and better caching for apps.

React’s new Context API will make it easier for developers to extract information from nested components. The old context API was difficult to work with as you had to specify your contexts upfront, but now this can happen at any time during your app’s lifecycle.

New features in React 18 will also include support for web spec updates such as HTTP2 and WebAssembly – meaning that not only are you developing a fastest runtime environment possible but also the most efficient ecosystem.

One of the biggest additions is the introduction of Hooks, which is a way to turn methods into React components. Developers can, for example, reuse code to take advantage of the View and use the same code in other components.

Now we have discussed many new update of REACT 18 below, let’s look one by one:

Portals in React

Portals are a new concept in React 18 which allows you to render content in a different part of the DOM. Portals are only supported on React 18.

The React team has been working on porting the portals functionality from their experimental library, react-portal, to core. The main idea behind portals is that they can be used to render content outside of the DOM tree and then display it inside the DOM tree at an arbitrary location.

In React 18, the Portals API allows developers to render content from one DOM node in another DOM node. The Portal is a top-level API that allows you to render a child into any DOM node that exists in the same tree.

import ReactDOM from 'react-dom'
export default function App() {
  return ReactDOM.createPortal(
    <h1>Portal demo</h1>,
    document.getElementById('portal')
  )
}

React Fiber

React Fiber is a new rendering engine that will be released with React 18. It is a complete rewrite of the React codebase, and it’s designed to be more efficient and flexible than the previous version.

Fiber has been in development for over two years, and it’s designed to be more efficient and flexible than the previous version. The main goal of Fiber is to improve user experience by reducing jank and choppiness in animations.

React Fiber promises to offer:

React Fiber is a new way to build React applications. It’s faster, more stable, and has a better developer experience.

React Fiber is the latest evolution of React. It’s a complete rewrite of how React works internally, to make it more performant and flexible for future development.

It provides two major benefits over the previous version of React:

Improved performance: This is due to a change in how React handles updates on the virtual DOM. Previously, when an update happened, all components would be checked for changes and re-rendered if needed; now only the components that actually need updating will be re-rendered. This leads to fewer checks and less work overall for each update – so your app will run faster!

Improved stability: By only re-render

export default function App() {
  const [data, setData] = useState(null)
  return [<Header data={data} />, <Footer />]
}

function Header(props) {

  return <h1>Header</h1>
}

function Footer() {
  return <h1>Footer</h1>
}

Concurrent Rendering

Concurrent Rendering is a new feature in React 18 which allows for the rendering of components in parallel.

This feature will help improve the performance of the React application by splitting the work into multiple threads. It will also reduce the chances of frame drops, which can happen when there is a high load on the CPU.

This is a big deal for performance, because it means that the browser can do more work in a given amount of time. For example, if your animation has 10 frames, then Concurrent Rendering will allow 10 frames to be rendered in the same amount of time it would take to render one frame without Concurrent Rendering.

Automatic Batching

Automatic Batching is a new feature in React 18 that allows you to batch your component updates. Automatic batching is a new feature in React 18. It allows you to batch updates to multiple components with a single call to setState.

This is done by grouping together the updates so that they will be applied in one go, reducing the number of calls to setState and reducing the amount of time it takes for your app to respond to user interactions. The batching process will execute all the changes needed for a component and its children in one go.

This is achieved by using the ReactDOM.unstable_batchedUpdates() function, which takes an array of objects representing the components and their desired props, and returns an object with an array of DOM nodes that have been updated.

export default function App() {
  const [increase, setIncrease] = useState(0);
  const [decrease, setDecrease] = useState(0);
  const handleOnClick = () => {
    setIncrease(increase + 1);
    setDecrease(decrease - 1);
  };
  const handleOnClickAsync = () => {
    fetch('https://jsonplaceholder.typicode.com/todos/1').then(() => {
      setIncrease(increase + 1);
      setDecrease(decrease - 1);
    });
  };
  return <>
    <button onClick={() => { handleOnClickAsync(); }}> Click Me! </button> {increase} {decrease}
  </>
}

Transitions

Transitions in REACT 18 are a way to add interest, context, and meaning to your presentation. They can be used to change slides or to signal a break in content.

New Hooks in React

New Hooks in React are a new way to create React components. They are a set of functions that developers can use to make their own hooks, and they provide a more flexible way of writing React code.

React Hooks is an experimental project that allows you to create React components without writing any class components or managing their state. You can also use it for stateful components, which are usually made with classes, but this time you can do it with a function.

useId

The useId() hook is an easy way to generate a random and unique ID number in React 18. The useId hook aids in generating a random and unique identifier on both the client-side and server-side. The previous one was called “useOpaqueIdentifier” in React.

import { useId } from "react"

export default function App() {
  const FullName = useId()
  const EmailAddress = useId()
  console.log(FullName, EmailAddress)
  return (
    <>
      <h1>Hello Acil Technologies</h1>
      <label htmlFor={FullName}>Full Name</label>
      <input type="text" id={FullName} name="Full Name" />
      <label htmlFor={EmailAddress}>Email Address</label>
      <input type="text" id={EmailAddress} name="Email Address" />
    </>)
}

useTransition

The use of Transition hooks allows us to specify certain state updates as less important. React’s useTransition is a built-in function that can be used to animate the changes between two components. It can be used as a class, or it can be used as an inline function.

React’s useTransition is not to be confused with TransitionGroup. React’s useTransition provides animations for the changes between two components and TransitionGroup provides animations for the changes within one component.

React’s useTransition has a few different animation types: crossFade, flip, slide, and fade.

import { useId, useTransition, useState } from "react"

export default function App() {
  const [isTransitionPending, startTransition] = useTransition();
  const [data, setData] = useState('');

  updateFilterHandler =  (e) => {
    startTransition(() => {
      setData(e.target.value);
    });
  }

  console.log(filterTerm)
  return (
    <div>
      <input type="text" onChange={updateFilterHandler} />
      {isTransitionPending && <p>Value Updating</p>}
    </div>
  )
}

useDeferredValue

The React.useDeferredValue() function is used to achieve asynchronous rendering. This function returns a Deferred value that can be used in the render() method to specify a loading indicator for an element.

import { useId, useTransition, useState, useDeferredValue } from "react"

export default function App() {
  const [isTransitionPending, startTransition] = useTransition();
  const [data, setData] = useState([]);

  const updateFilterHandler =  (e) => {
    startTransition(() => {
      setData([...data, e.target.value]);
    });
  }

  console.log(data)
  return (
    <div>
      <input type="text" onChange={updateFilterHandler} />
      {isTransitionPending && <p>Value Updating</p>}
      <ProductListing products={data} />
    </div>
  )
}


const ProductListing = ({ products }) => {
  const getProducts = useDeferredValue(products);
  return (
    <ul>
      {getProducts.map((product) => (
        <li>{product}</li>
      ))}
    </ul>
  );
}

useSyncExternalStore

The useSyncExternalStore use property in React has been introduced to solve the problem of data inconsistency in a shared system. It allows the developer to set up a store which will be updated with new data when it becomes available.

useInsertionEffect

The use of react useInsertionEffect is a simple and popular way to handle the insertion of DOM elements.

react useInsertionEffect is a React component that provides a declarative way to handle the insertion and removal of DOM nodes. It handles both the insertion and removal of DOM nodes.

Related Blogs

  • tailwind
    March 1, 2022

    What is Tailwind CSS and how to use it?

    Tailwind CSS is an open-source, responsive utility framework for rapidly developing websites. Tailwind was first released in 2015 by Adam Denning, the crea ....

  • January 31, 2022

    Future of TypeScript

    TypeScript is a superset of JavaScript. In other words, TypeScript is a typed superset of JavaScript. It is a programming language that compiles to JavaScr ....

  • April 21, 2023

    ReactJS vs React Native: Which One to Choose

    ReactJS and React Native are two popular technologies for building web and mobile applications, respectively. Both were developed and are maintained by Fac ....