React Basics~Render Performance/ useTransition

Suppose that we are displaying a large number of data, such as 10thousands of data, there is often a delay in puuting next value to the input field.
In this case, when we enter a value, the screen displays filtered data.
But then, a problem that occur…


This content originally appeared on DEV Community and was authored by Ogasawara Kakeru

  • Suppose that we are displaying a large number of data, such as 10thousands of data, there is often a delay in puuting next value to the input field.

  • In this case, when we enter a value, the screen displays filtered data.

  • But then, a problem that occurs is the delay in displaying the next action such as input next value to input field due to handling too much data.

・src/Example.js

import { useState } from "react";

const generateDummyItem = (num) => {
  return new Array(num).fill(null).map((item, index) => `item ${index}`);
};

const dummyItems = generateDummyItem(10000);

const Example = () => {
  const [filterVal, setFilterVal] = useState("");

  const changeHandler = (e) => {
      setFilterVal(e.target.value);
  };

  return (
    <>
      <input type="text" onChange={changeHandler} />
      {isPending && <div>Loading...</div>}
      <ul>
        {dummyItems
          .filter((item) => {
            if (filterVal === "") return true;
            return item.includes(filterVal);
          })
          .map((item) => (
            <li key={item}>{item}</li>
          ))}
      </ul>
    </>
  );
};

export default Example;

  • To solve the problem, we can wrap the setFilterVal function with a startTransition.
  const changeHandler = (e) => {
    startTransition(() => {
      setFilterVal(e.target.value);
    })
  };
  • The startTransition makes a function delay to be executed within it.

  • Thanks to this feature, we can easily move on to the next value in the input field.

・Before input
Image description

・After input
Image description


This content originally appeared on DEV Community and was authored by Ogasawara Kakeru


Print Share Comment Cite Upload Translate Updates
APA

Ogasawara Kakeru | Sciencx (2024-10-17T23:24:17+00:00) React Basics~Render Performance/ useTransition. Retrieved from https://www.scien.cx/2024/10/17/react-basicsrender-performance-usetransition/

MLA
" » React Basics~Render Performance/ useTransition." Ogasawara Kakeru | Sciencx - Thursday October 17, 2024, https://www.scien.cx/2024/10/17/react-basicsrender-performance-usetransition/
HARVARD
Ogasawara Kakeru | Sciencx Thursday October 17, 2024 » React Basics~Render Performance/ useTransition., viewed ,<https://www.scien.cx/2024/10/17/react-basicsrender-performance-usetransition/>
VANCOUVER
Ogasawara Kakeru | Sciencx - » React Basics~Render Performance/ useTransition. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/17/react-basicsrender-performance-usetransition/
CHICAGO
" » React Basics~Render Performance/ useTransition." Ogasawara Kakeru | Sciencx - Accessed . https://www.scien.cx/2024/10/17/react-basicsrender-performance-usetransition/
IEEE
" » React Basics~Render Performance/ useTransition." Ogasawara Kakeru | Sciencx [Online]. Available: https://www.scien.cx/2024/10/17/react-basicsrender-performance-usetransition/. [Accessed: ]
rf:citation
» React Basics~Render Performance/ useTransition | Ogasawara Kakeru | Sciencx | https://www.scien.cx/2024/10/17/react-basicsrender-performance-usetransition/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.