Open In App

New Hooks in React 18

React’s state, representing dynamic component data, is integral for effective component management. Initially confined to class components and managed using this.setState, the introduction of React Hooks in version 16.8 extended state usage to functional components. Hooks, functions enabling state and lifecycle management in functional components, provide a more versatile approach to React development.

There are several built-in hooks in React, today we are going to see new React hooks that are introduced in React 18.



useId:

useId hook is one of the simplest hooks in react js and it is used to generate unique id’s for related elements on both client-side and server-side. The useOpaqueIdentifierhook was used before useId but it did contain some bugs and had its limitations with react 18 update react introduced useId with bug fixes.



Importing useId hook:

import { useId } from "react"

Syntax:

const id = useId()

useId does not take any parameters.

Example: below is the practical example of useId hook




import Component from "./Component";
 
function App() {
    return (
        <div className="App">
            <Component />
            <p>
                This is Paragraph
            </p>
            <Component />
        </div>
    );
}
 
export default App;




import React, { useId } from 'react'
 
function Component() {
    const id = useId()
    return (
        <div>
            <label htmlFor={id}>Field 1:</label>
            <input
                type="text"
                id={id}
            />
        </div>
    )
}
 
export default Component

Output:

Output

useDeferredValue:

Importing:

import { useDeferredValue } from 'react'

Syntax:

const deferredValue = useDeferredValue(value);

Example: below is the practical example of useDeferredValue hook




import React, { useState } from 'react';
import DisplayInput from './DisplayInput';
 
function App() {
    const [userInput, setUserInput] = useState('');
 
    const handleInputChange = (e) => {
        setUserInput(e.target.value);
    };
 
    return (
        <div>
            <label htmlFor="userInput">
                Enter Text:
            </label>
            <input
                type="text"
                id="userInput"
                value={userInput}
                onChange={handleInputChange} />
            <DisplayInput input={userInput} />
        </div>
    );
}
 
export default App;




import React,
{ useDeferredValue } from 'react';
 
function DisplayInput({ input }) {
    const deferredValue =
        useDeferredValue(input);
 
    const renderInputs = () => {
        const inputs = [];
        for (let i = 0; i < 20000; i++) {
            inputs.push(
                <div key={i}>
                    {deferredValue}
                </div>
            );
        }
        return inputs;
    };
 
    return (
        <div>
            <h2>
                Displaying
                {deferredValue}
            </h2>
            {renderInputs()}
        </div>
    );
}
 
export default DisplayInput;

Output: There is no delay in updation of input in input box.

Output

useTransition:

Import:

import { useTransition } from 'react'

Syntax:

const [isPending, startTransition] = useTransition()

Example: below is the practical example of useTransition hook




import React from 'react';
import SearchList from './SearchList';
 
function App() {
    return (
        <div>
            <SearchList />
        </div>
    );
}
 
export default App;




import React, {
    useState,
    useTransition
} from 'react';
 
const namesList = [
    'Alice',
    'Bob',
    'Charlie',
    'David',
    'Eva',
    'Frank'
    // ... Add more names as needed
];
 
function SearchList() {
    const [searchTerm, setSearchTerm] = useState('');
    const [filteredNames, setFilteredNames] = useState(namesList);
 
    const [isPending, setTransition] = useState();
 
    const handleSearch = (e) => {
        const term = e.target.value.toLowerCase();
        setSearchTerm(term);
 
        setTransition(() => {
            const filtered =
                namesList.filter(
                    name =>
                        name.toLowerCase()
                            .includes(term)
                );
            setFilteredNames(filtered);
        })
    };
 
    return (
        <div>
            <label htmlFor="search">
                Search:
            </label>
            <input
                type="text"
                id="search"
                value={searchTerm}
                onChange={handleSearch}
                placeholder="Type to search names" />
            <ul>
                {
                    filteredNames.map((name, index) => (
                        <li key={index}>
                            {name}
                        </li>
                    ))
                }
            </ul>
        </div>
    );
}
 
export default SearchList;

Output:

useSyncExternalStore:

Import:

import { useSyncExternalStore } from 'react'

Syntax:

const variable_name = useSyncExternalStore(subscribe, getSnapshot, [getServerSnapshot]?)

Example: below is the practical example of useSyncExternalStore hook




import React from 'react';
import ResizableElement
    from './BatteryStatusIndicator';
 
function App() {
    return (
        <div>
            <ResizableElement />
        </div>
    );
}
 
export default App;




import { useSyncExternalStore } from 'react'
 
export default function ResizableElement() {
    const subscribe = (listener) => {
        window.addEventListener('resize', listener)
        return () => {
            window.removeEventListener('resize', listener)
        }
    }
    const width =
        useSyncExternalStore(subscribe,
            () => window.innerWidth);
    return (
        <div>
            <p>Size: {width}</p>
        </div>
    )
}

Output:

Output

useInsertionEffect:

Import:

import { useInsertionEffect } from 'react';

Syntax:

useInsertionEffect(()=>{
    // Insert dynamic styles before layout effects fire
    return()=>{
        //cleanup function
    }
}, [])

Example: This exampe implements UseInsertionEffectHook in React




import React  from 'react';
import UseInsertionEffectHook from './BatteryStatusIndicator';
 
function App() {
  return (
    <div>
      <UseInsertionEffectHook/>
      <br/>
      <br/>
      <button>First</button>
      <button>Second</button>
    </div>
  );
}
 
export default App;




import { useInsertionEffect, useState } from "react";
 
export default function UseInsertionEffectHook() {
    const [theme, setTheme] = useState('dark')
 
    useInsertionEffect(() => {
        const styleRule = getStyleRule(theme);
 
        document.head.appendChild(styleRule);
 
        return () => document.head.removeChild(styleRule)
    }, [theme])
 
    return <button
        onClick={
            () =>
                setTheme(theme === 'dark' ?
                    'white' : 'dark')}>
        Change theme
    </button>
}
 
const getStyleRule = (theme) => {
    const tag = document.createElement('style')
    tag.innerHTML = `
    button {
      color: ${theme === 'dark' ? 'white' : 'black'};
      background-color :
    ${theme === 'dark' ? 'black' : 'white'};
    }
  `
    return tag
}

Output:

Output


Article Tags :