Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #61- Add useDebounce Custom hook #67

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rray524
Copy link
Collaborator

@rray524 rray524 commented Dec 24, 2024

Checklist:

  • Added useDebounce custom hook

Resolves #61

How to use this useDebounce hook?

Usage Example of Theme Toggling:

import React, { useState, useEffect } from 'react';
import useDebounce from './useDebounce'; // Path to your custom Hook

const SearchInput = () => {
  const [query, setQuery] = useState('');
  const debouncedQuery = useDebounce(query, 500); // Debounce with a 500ms delay

  // Simulate an API call triggered on the debounced value
  useEffect(() => {
    if (debouncedQuery) {
      console.log('Fetching results for:', debouncedQuery);
      // Example: Call an API with the debounced query
      // fetchResults(debouncedQuery);
    }
  }, [debouncedQuery]);

  return (
    <div>
      <input
        type="text"
        value={query}
        onChange={(e) => setQuery(e.target.value)}
        placeholder="Type to search..."
      />
      <p>Search Query: {query}</p>
      <p>Debounced Query: {debouncedQuery}</p>
    </div>
  );
};

export default SearchInput;


Description

The useDebounce Hook is a custom React Hook that delays updating a value until after a specified delay. It is useful for optimizing performance in scenarios like search inputs or API calls by preventing excessive updates caused by frequent changes. This Hook ensures actions are triggered only after the user has stopped typing or interacting for a set period of time.

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature Request]: Create useDebounce Hook in React
2 participants