aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/hooks/use-state-change.tsx
blob: 59ae0dfb541176378ddd34250d206f7104d52b9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.
 */
export const useStateChange = <T,>(initial: T) => {
  const [state, setState] = useState<T>(initial);

  useEffect(() => {
    setState(initial);
  }, [initial]);

  return [state, setState] as const;
};