Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/useThrottle.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

React hooks that throttle.

`useThrottle` keeps publishing values when effects are replayed in React Strict Mode.

## Usage

```jsx
import React, { useState } from 'react';
import { useThrottle, useThrottleFn } from 'react-use';

const Demo = ({value}) => {
const Demo = ({ value }) => {
const throttledValue = useThrottle(value);
// const throttledValue = useThrottleFn(value => value, 200, [value]);

Expand Down
2 changes: 2 additions & 0 deletions src/useThrottle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const useThrottle = <T>(value: T, ms: number = 200) => {

useUnmount(() => {
timeout.current && clearTimeout(timeout.current);
timeout.current = undefined;
hasNextValue.current = false;
});

return state;
Expand Down
7 changes: 6 additions & 1 deletion stories/useThrottle.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ const Demo = () => {

storiesOf('Side effects/useThrottle', module)
.add('Docs', () => <ShowDocs md={require('../docs/useThrottle.md')} />)
.add('Demo', () => <Demo />);
.add('Demo', () => <Demo />)
.add('Strict Mode', () => (
<React.StrictMode>
<Demo />
</React.StrictMode>
));
142 changes: 141 additions & 1 deletion tests/useThrottle.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,50 @@
import { renderHook } from '@testing-library/react-hooks';
import React from 'react';
import { act, renderHook } from '@testing-library/react-hooks';
import useThrottle from '../src/useThrottle';

const renderWithEffectReplay = () => {
const originalEffect = React.useEffect;
const effects: React.EffectCallback[] = [];
let cleanups: ReturnType<React.EffectCallback>[] = [];
let effectIndex = 0;

jest.spyOn(React, 'useEffect').mockImplementation((effect, dependencies) => {
const index = effectIndex++;
effects[index] = effect;
originalEffect(() => {
const cleanup = effect();
cleanups[index] = cleanup;
return cleanup;
}, dependencies);
});

const hook = renderHook(
(value) => {
effectIndex = 0;
return useThrottle(value, 100);
},
{ initialProps: 0 }
);

return {
...hook,
replayEffects: () => {
act(() => {
cleanups.forEach((cleanup) => cleanup && cleanup());
cleanups = effects.map((effect) => effect());
});
},
};
};

describe('useThrottle', () => {
beforeAll(() => {
jest.useFakeTimers();
});

afterEach(() => {
jest.clearAllTimers();
jest.restoreAllMocks();
});

afterAll(() => {
Expand Down Expand Up @@ -72,4 +109,107 @@ describe('useThrottle', () => {
jest.advanceTimersByTime(100);
expect(hook.result.current).toBe(0);
});

it('should publish the latest pending value in each throttle interval', () => {
const { result, rerender } = renderHook((value) => useThrottle(value, 100), {
initialProps: 0,
});

rerender(1);
rerender(2);
act(() => {
jest.advanceTimersByTime(100);
});
expect(result.current).toBe(2);

rerender(3);
rerender(4);
act(() => {
jest.advanceTimersByTime(100);
});
expect(result.current).toBe(4);
act(() => {
jest.advanceTimersByTime(100);
});
expect(jest.getTimerCount()).toBe(0);

rerender(5);
expect(result.current).toBe(5);
});

it('should restart the timeout after effect cleanup and setup are replayed', () => {
const { result, rerender, replayEffects } = renderWithEffectReplay();

replayEffects();
expect(jest.getTimerCount()).toBe(1);
rerender(1);
expect(result.current).toBe(0);
act(() => {
jest.advanceTimersByTime(100);
});
expect(result.current).toBe(1);

rerender(2);
act(() => {
jest.advanceTimersByTime(100);
});
expect(result.current).toBe(2);
});

it('should discard pending values when effects are replayed', () => {
const { result, rerender, replayEffects } = renderWithEffectReplay();

rerender(1);
replayEffects();
expect(result.current).toBe(1);
expect(jest.getTimerCount()).toBe(1);

act(() => {
jest.advanceTimersByTime(100);
});
expect(result.current).toBe(1);
expect(jest.getTimerCount()).toBe(0);

rerender(2);
expect(result.current).toBe(2);
});

it('should keep a single timeout across repeated effect replays', () => {
const { result, rerender, replayEffects, unmount } = renderWithEffectReplay();

replayEffects();
replayEffects();
replayEffects();
expect(jest.getTimerCount()).toBe(1);

rerender(1);
act(() => {
jest.advanceTimersByTime(100);
});
expect(result.current).toBe(1);

unmount();
expect(jest.getTimerCount()).toBe(0);
});

it('should replay idle effects and clean up without an active timeout', () => {
const { result, rerender, replayEffects, unmount } = renderWithEffectReplay();

act(() => {
jest.advanceTimersByTime(100);
});
expect(jest.getTimerCount()).toBe(0);

replayEffects();
expect(jest.getTimerCount()).toBe(1);
rerender(1);
act(() => {
jest.advanceTimersByTime(200);
});
expect(result.current).toBe(1);
expect(jest.getTimerCount()).toBe(0);

unmount();
expect(jest.getTimerCount()).toBe(0);
});
});