react#useTransition JavaScript Examples

The following examples show how to use react#useTransition. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.js    From React-Hooks with MIT License 6 votes vote down vote up
Component = () => {
  const [isPending, startTransition] = useTransition();
  const [inputValue, setInputValue] = React.useState("");
  const [filteredProducts, setFilteredProducts] = React.useState(data);

  const handleChange = (e) => {
    const { value } = e.target;
    // startTransition(() => {
    // });
    setInputValue(value);
  };

  React.useEffect(() => {
    if (!inputValue) {
      setFilteredProducts(data);
    }
    setFilteredProducts(data.filter((el) => el.text.includes(inputValue)));
  }, [inputValue]);
  return (
    <div className='mt-4'>
      <div className='d-flex justify-content-center'>
        <input
          type='text'
          className='input-group-text bg-white'
          onChange={handleChange}
        />
      </div>
      {isPending ? (
        <h4 color='blue'>Loading List</h4>
      ) : (
        <ProductList products={filteredProducts} />
      )}
    </div>
  );
}