Coder Social home page Coder Social logo

use-effect-debounced's Introduction

useEffectDebounced

This repository demonstrates creating a custom hook called useEffectDebounced. It works just like useEffect except the function you pass to useEffectDebounced only be executed after a user-specified amount of time (and only if the the parameters on which the effect depends do not change).

Implementation

the useEffectDebounced hook is implemented as follows:

import { useEffect } from "react";

const useEffectDebounced = (func, values, time) => {
  useEffect(() => {
    let db = setTimeout(() => {
      func();
    }, time);

    return () => clearTimeout(db);
  }, values);
};

export default useEffectDebounced;

The passed function func is executed in a setTimeout after the user specified time. However, if one of the values the hook depends on is changed, the clean-up function () => clearTimeout(db) is executed, meaning func will not execute.

Example Usage

This application includes an example usage in which a textbox is used to filter through a list of countries. The filtering is done in the useEffectDebounced hook, meaning the filtering will not occur until the specified period of time has passed without additional changes to the textbox.

import React, { useState } from "react";
import useEffectDebounced from "./useEffectDebounced";
import countries from "./countries";

const App = () => {
  const [filteredCountries, setFilteredCountries] = useState(countries);
  const [search, setSearch] = useState("");

  useEffectDebounced(
    () => {
      const filtered = countries.filter(country =>
        country.name.toLowerCase().includes(search.toLowerCase())
      );
      setFilteredCountries(filtered);
    },
    [search],
    1000
  );

  return (
    <div>
      <input value={search} onChange={e => setSearch(e.target.value)} />
      <ul>
        {filteredCountries &&
          filteredCountries.map((country, index) => {
            return <li key={index}>{country.name}</li>;
          })}
      </ul>
    </div>
  );
};

export default App;

Countries being filtered with debounce

Feedback

I would appreciate any feedback you have on this custom hook!

use-effect-debounced's People

Contributors

nas5w avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

puckey

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.