This repository has been archived by the owner on Jan 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1354 from Shopify/debounced-value
Adds useDebouncedValue hook
- Loading branch information
Showing
5 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {useEffect, useState} from 'react'; | ||
|
||
interface DebouncedOptions { | ||
timeoutMs: number; | ||
} | ||
|
||
const DEFAULT_DELAY = 500; | ||
|
||
export function useDebouncedValue<T>( | ||
value: T, | ||
{timeoutMs}: DebouncedOptions = {timeoutMs: DEFAULT_DELAY}, | ||
) { | ||
const [state, setState] = useState<T>(value); | ||
|
||
useEffect(() => { | ||
const timeout = window.setTimeout(() => { | ||
setState(value); | ||
}, timeoutMs); | ||
|
||
return () => { | ||
window.clearTimeout(timeout); | ||
}; | ||
}, [value, timeoutMs]); | ||
|
||
return state; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import React, {useState} from 'react'; | ||
import {mount} from '@shopify/react-testing'; | ||
import {timer} from '@shopify/jest-dom-mocks'; | ||
|
||
import {useDebouncedValue} from '../debounced'; | ||
|
||
const ONE_SECOND = 1000; | ||
const HALF_A_SECOND = ONE_SECOND / 2; | ||
|
||
describe('useDebouncedValue', () => { | ||
beforeEach(() => { | ||
timer.mock(); | ||
}); | ||
|
||
afterEach(() => { | ||
timer.restore(); | ||
}); | ||
|
||
it('returns the first value when called once', () => { | ||
const wrapper = setup({delay: ONE_SECOND, value: 'something'}); | ||
|
||
expect(wrapper).toContainReactText('something'); | ||
}); | ||
|
||
it('returns the debounced value', () => { | ||
const initialValue = 'something'; | ||
const newValue = 'change'; | ||
const wrapper = setup({delay: ONE_SECOND, value: initialValue}); | ||
|
||
wrapper.setProps({value: newValue}); | ||
|
||
wrapper.act(() => { | ||
timer.runTimersToTime(HALF_A_SECOND); | ||
}); | ||
|
||
expect(wrapper).toContainReactText(initialValue); | ||
|
||
wrapper.act(() => { | ||
timer.runTimersToTime(ONE_SECOND); | ||
}); | ||
|
||
expect(wrapper).toContainReactText(newValue); | ||
}); | ||
}); | ||
|
||
function setup({delay, value}) { | ||
const wrapper = mount(<MockComponent delay={delay} value={value} />); | ||
|
||
return wrapper; | ||
} | ||
|
||
function MockComponent({value, delay}) { | ||
const debouncedValue = useDebouncedValue(value, {timeoutMs: delay}); | ||
return <div>{debouncedValue}</div>; | ||
} |