blob: 063fb8e452cbe28bba54c52ed509b33d1b419e0f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import { useEffect, useState } from 'react';
/**
* Use React useState hook and update it if initial data change.
*
* @param initial - The initial value.
* @returns The state and a setter.
*/
const useStateChange = <T,>(initial: T) => {
const [state, setState] = useState<T>(initial);
useEffect(() => {
setState(initial);
}, [initial]);
return [state, setState] as const;
};
export default useStateChange;
|