Skip to content

samanmohamadi/use-debounced-effect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

useDebouncedEffect React Hook

Install it with yarn:

yarn add use-debounced-effect

Or with npm:

npm i use-debounced-effect --save

Shorthand

useDebouncedEffect(callback, delay, dependencies)

import  React, { useState } from  'react';
import  useDebouncedEffect  from  'use-debounced-effect';

export  default  function  Input() {
	const [term, setTerm] = useState('');

	useDebouncedEffect(()=>{
	console.log(term); // debounced 1sec
	// call search api ...
	// return () => maybe cancel prev req
	}, 1000 ,[term]);

	return (
		<input  onChange={(e) => setTerm(e.target.value)}  />
	);
}

Advanced

useDebouncedEffect(callback, config, dependencies)

Config

Name type Description Default/Fallback
timeout number debounce delay 0
ignoreInitialCall boolean ignore first effect call true

Example

	useDebouncedEffect(()=>{
		console.log(term); // debounced 1sec
		// call search api ...
		// return () => maybe cancel prev req
	}, 
	{ 
		timeout: 1000,
		ignoreInitialCall: false
	}
	,[term]);

}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •