We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c73e92f commit 16f023fCopy full SHA for 16f023f
src/useSetState.ts
@@ -1,12 +1,15 @@
1
-import { useState } from 'react';
+import { useState, useCallback } from 'react';
2
3
const useSetState = <T extends object>(
4
initialState: T = {} as T
5
): [T, (patch: Partial<T> | ((prevState: T) => Partial<T>)) => void] => {
6
const [state, set] = useState<T>(initialState);
7
- const setState = patch => {
8
- set(prevState => Object.assign({}, prevState, patch instanceof Function ? patch(prevState) : patch));
9
- };
+ const setState = useCallback(
+ patch => {
+ set(prevState => Object.assign({}, prevState, patch instanceof Function ? patch(prevState) : patch));
10
+ },
11
+ [set]
12
+ );
13
14
return [state, setState];
15
};
0 commit comments