- Fix behaviour for strictMode react when leading is set to true and trailing is true
- Removed
peerDependency
part frompackage.json
as NPM cannot correctly resolvepeerDependency
for beta and rc versions: see https://stackoverflow.com/questions/67934358/npm-including-all-range-of-pre-release-when-defining-peer-dependency for context
- Fixed:
isPending
does not reset the state if the tracked value hasn't changed.. See #178
- Fixed flush method return args, thanks to @h
- Major breaking change: replaced
index.modern.js
withindex.mjs
. Might require a little change in your build pipeline - Major breaking change: New option
debounceOnServer
: if you put the option to true, it will run debouncing on server (viasetTimeout
). The new option can break your current server behaviour, as v9.x, it runs all the time and might cause unnessesary server CPU utilisation. Now, by default, debounced callbacks do not happen on server. - Minor breaking change: Replaced
useState
foruseDebounce
withuseReducer
. It might lead to reduced amount of re-renders, as useState is known to have excess re-renders in some corner: https://stackoverflow.com/questions/57652176/react-hooks-usestate-setvalue-still-rerender-one-more-time-when-value-is-equal - Minor breaking change:
useDebouncedCallback
now updates function to call asap. Meaning, if you re-called the hook and it should trigger immediately, it will trigger the newest function all the time. - Lib size: 914 B: index.js.gz 851 B: index.js.br 883 B: index.mjs.gz 826 B: index.mjs.br 938 B: index.module.js.gz 873 B: index.module.js.br 989 B: index.umd.js.gz 919 B: index.umd.js.br
- [Internal] Replaced Enzyme with @testing-library
- [Internal] yarn classic => npm
- [Internal] Updated devDependencies
- Reverted 9.0.0. We will revisit these changes later
- Moved use-debounce to support modules see issue Thanks to @matewilk
- breaking change The path to
dist/index.js
is changed. Now it'sdist/index.cjs
.
- Added
types
to package json to mitigate microsoft/TypeScript#49160. #138 Thanks to @wuzzeb
- Added type exports. #136 Thanks to @tryggvigy
- Improved code comments. #135 Thanks to @tryggvigy
- update library exports section to make exports work correctly with jest@28
- breaking change
useDebounce
changed its build system to microbundle. For now we have several entries:
index.js
is for commonJS approach
index.modern.js
for esnext module system
index.umd.js
for UMD.
All the files are in dist
folder.
If you have any paths which have esm
or lib
, please, replace them to dist
:
Before:
import useDebounceCallback from 'use-debounce/lib/useDebounceCallback';
After:
import { useDebounceCallback } from 'use-debounce';
- Fixed issue with incorrect VSCode autocomplete #131 Thanks to @c-ehrlich for reporting
- Fixed
useDebounce
behaviour with react-devtools tab when devtools have a component withuseDebounce
oruseDebounceCallback
opened. #129 Thanks to @alexniarchos for reporting - Fixed issue with
leading: true
#124 Thanks to @mntnoe for reporting
debounced
object now is preserved foruse-debounce
between the renders. Thanks to @msharifi99 for reporting.
- breaking change
useDebounce
hook changedisPending
behavior fromasync
reacting to the sync. NowisPending
returnsTrue
as soon as the new value is sent to the hook. - Dev dependencies updated
-
breaking change: removed
callback
field, instead of thisuseDebouncedCallback
anduseThrottledCallback
returns a callable function: Old:const { callback, pending } = useDebouncedCallback(/*...*/); // ... debounced.callback();
New:
const debounced = useDebouncedCallback(/*...*/); // ... debounced(); /** * Also debounced has fields: * { * cancel: () => void * flush: () => void * isPending: () => boolean * } * So you can call debounced.cancel(), debounced.flush(), debounced.isPending() */
It makes easier to understand which cancel \ flush or isPending is called in case you have several debounced functions in your component
-
breaking change: Now
useDebounce
,useDebouncedCallback
anduseThrottledCallback
hasisPending
method instead ofpending
Old:
const { callback, pending } = useDebouncedCallback(/*...*/);
New:
const { isPending } = useDebouncedCallback(/*...*/); /** * { * cancel: () => void * flush: () => void * isPending: () => boolean * } */
-
get rid of
useCallback
calls -
improve internal typing
-
decrease the amount of functions to initialize each
useDebouncedCallback
call -
reduce library size:
Whole library: from 946 B to 899 B === 47 B useDebounce: from 844 to 791 === 53 B useDebouncedCallback: from 680 to 623 === 57 B useThrottledCallback: from 736 to 680 === 56 B
- prevent having ininite setTimeout setup when component gets unmounted #97
- function type works correctly with
useDebounce
now. #95 Thanks to @csu-feizao
- Added
useThrottledCallback
— wait
param is optional. If you don't provide a wait argument, use-debounce will postpone a callback with requestAnimationFrame if it's in browser environment, or through setTimeout(..., 0) otherwise.
- Add an export for React Native
- Fix the export map (#84);
-
Add size-limit and configure it for esm modules. Now the size of the whole library is limited within 1 KB (thanks to @omgovich)
-
Add an export map to your package.json. (thanks to @omgovich)
-
Reduce bundle size (thanks to @omgovich): Before:
esm/index.js Size: 908 B with all dependencies, minified and gzipped esm/index.js Size: 873 B with all dependencies, minified and gzipped esm/index.js Size: 755 B with all dependencies, minified and gzipped
Now:
esm/index.js Size: 826 B with all dependencies, minified and gzipped esm/index.js Size: 790 B with all dependencies, minified and gzipped esm/index.js Size: 675 B with all dependencies, minified and gzipped
-
Add notes about returned value from
debounced.callback
and its subsequent calls: https://github.com/xnimorz/use-debounce#returned-value-from-debouncedcallback -
Add project logo (thanks to @omgovich):
- Fix typing to infer correct callback type (thanks to @lytc)
-
breaking change: Now
useDebouncedCallback
returns an object instead of array:Old:
const [debouncedCallback, cancelDebouncedCallback, callPending] = useDebouncedCallback(/*...*/);
New:
const debounced = useDebouncedCallback(/*...*/); /** * debounced: { * callback: (...args: T) => unknown, which is debouncedCallback * cancel: () => void, which is cancelDebouncedCallback * flush: () => void, which is callPending * pending: () => boolean, which is a new function * } */
-
breaking change: Now
useDebounce
returns an array of 2 fields instead of a plain array: Old:const [value, cancel, callPending] = useDebounce(/*...*/);
New:
const [value, fn] = useDebounce(/*...*/); /** * value is just a value without changes * But fn now is an object: { * cancel: () => void, which is cancel * flush: () => void, which is callPending * pending: () => boolean, which is a new function * } */
-
Added
pending
function to bothuseDebounce
anduseDebouncedCallback
which shows whether component has pending callbacks Example:function Component({ text }) { const debounced = useDebouncedCallback( useCallback(() => {}, []), 500 ); expect(debounced.pending()).toBeFalsy(); debounced.callback(); expect(debounced.pending()).toBeTruthy(); debounced.flush(); expect(debounced.pending()).toBeFalsy(); return <span>{text}</span>; }
For more details of these major changes you could check this commit https://github.com/xnimorz/use-debounce/commit/1b4ac0432f7074248faafcfe6248df0be4bb4af0 and this issue #61
- Fixed security alerts
- breaking change: Support lodash style throttling options for trailing+maxWidth. Thanks to @tryggvigy Example:
useDebouncedCallback(callback, 300, {
leading: true,
trailing: false,
maxWait: 300,
});
Where the trailing edge is turned off. Let's say the function is called twice in the first 300ms. Now debounced function to have been called once.
how to migrate: Please, check your traling: false
params with maxWait
option
-
breaking change: Now in case delay option is unset, it will be
requestAnimationFrame
delay -
breaking change: Now
debouncedCallback
fromuseDebouncedCallback
returns a value. In v3 it used to returnundefined
:
- Fix use-debounce so that it works correctly with react-native and next.js (as both of them use fast-refresh).
- Clear cache in build directory. Thanks to @wangcch
- update types, so that they are more convinient. Thanks to @astj
- Now
callPendings
wrapped with useCallback hook, so that the reference to the function would be the same. Thanks to @jfschwarz
useDebouncedCallback
anduseDebounce
now can configure bothleading
andtrailing
options. They are fully compatible withlodash.debounce
function https://lodash.com/docs/4.17.15#debounce.leading
by default is false, trailing by default is true. Examples: https://codesandbox.io/s/vigilant-bush-zrbzg https://github.com/xnimorz/use-debounce/blob/master/test/useDebouncedCallback.test.tsx#L29-L180
useDebounce
hascallPending
method. See https://github.com/xnimorz/use-debounce/blob/master/test/useDebounce.test.tsx#L276-L302 unit test for examples.
- Now package includes only nessesary files. Thanks to @vkrol
- Added optional
equalityFn
tooptions
object foruseDebounce
so that you can provide a custom equality function to the hook. Thanks to @seruco
- Added missed
esm
directory (thanks for reporting @FredyC) - Fixed import name (thanks for PR @neoromantic)
- Updated
eslint-utils
lib version due to security issue
- breaking change now,
cache
file renamed touseDebounce
andcallback
file renamed touseDebouncedCallback
. If you used to import file by its path:
import useDebounce from 'use-debounce/lib/cache';
import useDebouncedCallback from 'use-debounce/lib/callback';
it should be renamed to
import useDebounce from 'use-debounce/lib/useDebounce';
import useDebouncedCallback from 'use-debounce/lib/useDebouncedCallback';
It helps us to keep more descriptive names. Thanks to @vkrol #33
-
breaking change now,
useDebouncedCallback
executes the latest callback, which was sent to the hook (thanks for the report @alexandr-bbm #35) https://github.com/xnimorz/use-debounce/commit/eca14cc25b1f14bdd337a555127fd98c54ab7a5c
— Added types
field in package.json. Thanks to @nmussy
- Added leading calls param https://github.com/xnimorz/use-debounce#leading-calls thanks to @Pringels
- Updated dev-dependencies
- Rewrite to typescript
- breaking changes now,
useDebouncedCallback
doesn't havedeps
argument. If you want to cache your callback it's better to use:
const myCallback = useDebouncedCallback(
useCallback(() => {
/* do some stuff */
}, [value]),
500
);
- added
size-limit
to the project. - Reduce size of the library from 705 bytes to 352 bytes (50%)
- remove
react-dom
from peerDependencies (as you can use this library with react native).
useCallback
now memoize returned callback
- add
callPending
callback touseDebouncedCallback
method. It allows to call the callback manually if it hasn't fired yet. This method is handy to use when the user takes an action that would cause the component to unmount, but you need to execute the callback.
import React, { useState, useCallback } from 'react';
import useDebouncedCallback from 'use-debounce/lib/callback';
function InputWhichFetchesSomeData({ defaultValue, asyncFetchData }) {
const [debouncedFunction, cancel, callPending] = useDebouncedCallback(
(value) => {
asyncFetchData;
},
500,
[],
{ maxWait: 2000 }
);
// When the component goes to be unmounted, we will fetch data if the input has changed.
useEffect(
() => () => {
callPending();
},
[]
);
return (
<input
defaultValue={defaultValue}
onChange={(e) => debouncedFunction(e.target.value)}
/>
);
}
More examples are available here: https://github.com/xnimorz/use-debounce/commit/989d6c0efb4eef080ed78330233186d7b0c249e3#diff-c7e0cfdec8acc174d3301ff43b986264R196
The example with all features you can see here: https://codesandbox.io/s/4wvmp1xlw4
- add maxWait option. The maximum time func is allowed to be delayed before it's invoked:
import { useDebounce, useDebouncedCallback } from 'use-debounce';
...
const debouncedValue = useDebounce(value, 300, {maxWait: 1000});
const debouncedCallback = useDebouncedCallback(() => {...}, 300, [], {maxWait: 1000});
- add cancel callback (thanks to @thibaultboursier for contributing). Cancel callback removes func from the queue (even maxWait):
import { useDebounce, useDebouncedCallback } from 'use-debounce';
...
const [ debouncedValue, cancelValueDebouncingCycle ] = useDebounce(value, 1000);
const [ debouncedCallback, cancelCallback ] = useDebouncedCallback(() => {...}, 1000);
- [BREAKING] change the contact of use-debounce callback and value hooks:
Old:
import { useDebounce, useDebouncedCallback } from 'use-debounce';
...
const debouncedValue = useDebounce(value, 1000);
const debouncedCallback = useDebouncedCallback(() => {...}, 1000);
New:
import { useDebounce, useDebouncedCallback } from 'use-debounce';
...
const [ debouncedValue, cancelValueDebouncingCycle ] = useDebounce(value, 1000);
const [ debouncedCallback, cancelCallback ] = useDebouncedCallback(() => {...}, 1000);
You still can use only value and callback:
import { useDebounce, useDebouncedCallback } from 'use-debounce';
...
const [ debouncedValue ] = useDebounce(value, 1000);
const [ debouncedCallback ] = useDebouncedCallback(() => {...}, 1000);
- add use-debounce callback and use-debounce value. First one is useful for debouncing callbacks e.g. event handlers, second one is handy for debouncing a value such as search fields etc.