apps/mantine.dev/src/pages/hooks/use-debounced-state.mdx
import { UseDebouncedStateDemos } from '@docs/demos'; import { Layout } from '@/layout'; import { MDX_DATA } from '@/mdx';
export default Layout(MDX_DATA.useDebouncedState);
The use-debounced-state hook debounces value changes.
This can be useful when you want to perform a heavy operation based on React state,
for example, sending a search request. Unlike use-debounced-value, it
is designed to work with uncontrolled components.
defaultValue prop instead of value), for example, it does not render with every state change like a character typed in an input.useState internally.You can immediately update the value with the first call using { leading: true } options:
interface UseDebouncedStateOptions {
leading?: boolean;
}
type UseDebouncedStateReturnValue<T> = [T, (newValue: SetStateAction<T>) => void];
function useDebouncedState<T = any>(
defaultValue: T,
wait: number,
options?: UseDebouncedStateOptions,
): UseDebouncedStateReturnValue<T>
The UseDebouncedStateOptions and UseDebouncedStateReturnValue types are exported from the @mantine/hooks package;
you can import them in your application:
import type { UseDebouncedStateOptions, UseDebouncedStateReturnValue } from '@mantine/hooks';