# useThrottledEffect
Package: @mantine/hooks
Import: import { useThrottledEffect } from '@mantine/hooks';

## Usage

`use-throttled-effect` works like `useEffect`, but runs the effect at most once per `wait`
milliseconds while its dependencies keep changing. Use it for effects that follow a stream of
updates, for example scroll position or a slider value:

```tsx
import { useState } from 'react';
import { Slider, Stack, Text } from '@mantine/core';
import { useThrottledEffect } from '@mantine/hooks';

function Demo() {
  const [value, setValue] = useState(0);
  const [samples, setSamples] = useState<number[]>([]);

  useThrottledEffect(
    () => {
      setSamples((current) => [...current, value].slice(-12));
    },
    [value],
    500
  );

  return (
    <Stack>
      <Text size="sm">Drag the slider – the effect samples the value at most once per 500ms</Text>
      <Slider value={value} onChange={setValue} />
      <Text size="sm">Samples: {samples.join(', ') || '–'}</Text>
    </Stack>
  );
}
```


## Difference from useEffect

The effect runs immediately on mount and on the first change. Changes during the next `wait`
milliseconds run it once more at the end of that time with the latest values.

The effect may return a cleanup function. It is called before the next run and on unmount.
A pending run is canceled on unmount.

Unlike [use-debounced-effect](https://alpha.mantine.dev/llms/hooks-use-debounced-effect.md), the hook has no `leading` and `maxWait` options.

## Definition

```tsx
function useThrottledEffect(
  effect: React.EffectCallback,
  deps: React.DependencyList,
  wait: number
): void;
```
