Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

debounce in react native hooks

// create a debounced effect custom hook in file ./useDebouncedEffect.js

import { useEffect } from 'react';

export const useDebouncedEffect = (effect: any, deps: any, delay: any) => {
    useEffect(() => {
        const handler = setTimeout(() => effect(), delay);
        return () => clearTimeout(handler);
    }, [...deps || [], delay]);
};

// use it inside your component (I am using it in App.js)

import { useState } from "react";
import { useDebouncedEffect } from "./useDebouncedEffect";

const App = () => {
  const [value, setValue] = useState('')

  useDebouncedEffect(() => {
    // write your code here
    console.log(value)
    
  }, [value], 1000);
  
  const handleChange = (e) => {
    setValue(e.target.value);
  }

  return (
    <button onClick={handleChange}>{value}</button>
  )
}

export default App;

Comment

PREVIOUS NEXT
Code Example
Javascript :: flutter jsonDecode UTF8 
Javascript :: usehistory example 
Javascript :: javascript unicode decoder 
Javascript :: jquery on checkbox checked es6 
Javascript :: javascript find document body 
Javascript :: socket io broadcast to room 
Javascript :: how to select second element in jquery 
Javascript :: lip. dips *dipped. also mm bpi. -opp. -ditty 
Javascript :: javascript recorrer json 
Javascript :: typeerror object(...) is not a function react useParams 
Javascript :: print webpage in javascript 
Javascript :: when was react invented 
Javascript :: javascript make obj invisible 
Javascript :: event listener for element lost focus 
Javascript :: how to remove first element of array javascript 
Javascript :: javascript regex replace all 
Javascript :: split words in javascript 
Javascript :: convert csv to json python using pandas 
Javascript :: js find object from value in array 
Javascript :: node load file 
Javascript :: js exec vs match 
Javascript :: references another schema in sequelize 
Javascript :: jquery trigger click other element 
Javascript :: div onchange react 
Javascript :: change swiper-slide width angular 
Javascript :: Get child node index 
Javascript :: javascript json string 
Javascript :: How to show confirm message before delete using jquery 
Javascript :: javascript persistent storage 
Javascript :: javascript add required to input 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =