Skip to content

Commit 9aa18fb

Browse files
committed
fix(useDebouncedState): Add factory function overload to type, clarify setter purity
1 parent 69fa4aa commit 9aa18fb

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/useDebouncedState.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import useDebouncedCallback, {
55

66
/**
77
* Similar to `useState`, except the setter function is debounced by
8-
* the specified delay.
8+
* the specified delay. Unlike `useState`, the returned setter is not "pure" having
9+
* the side effect of scheduling an update in a timeout, which makes it unsafe to call
10+
* inside of the component render phase.
911
*
1012
* ```ts
1113
* const [value, setValue] = useDebouncedState('test', 500)
@@ -17,10 +19,11 @@ import useDebouncedCallback, {
1719
* @param delayOrOptions The milliseconds delay before a new value is set, or options object
1820
*/
1921
export default function useDebouncedState<T>(
20-
initialState: T,
22+
initialState: T | (() => T),
2123
delayOrOptions: number | UseDebouncedCallbackOptions,
2224
): [T, Dispatch<SetStateAction<T>>] {
2325
const [state, setState] = useState(initialState)
26+
2427
const debouncedSetState = useDebouncedCallback<Dispatch<SetStateAction<T>>>(
2528
setState,
2629
delayOrOptions,

0 commit comments

Comments
 (0)