aboutsummaryrefslogtreecommitdiff
path: root/modern/src/reactHelper.js
blob: 7503360a8a4194076931165d873c1e04ab4c2b07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { useRef, useEffect } from 'react';

export const usePrevious = (value) => {
  const ref = useRef();
  useEffect(() => {
    ref.current = value;
  });
  return ref.current;
};

export const useEffectAsync = (effect, deps) => {
  const ref = useRef();
  useEffect(() => {
    effect().then((result) => ref.current = result);
    return () => {
      const result = ref.current;
      if (result) {
        result();
      }
    };
  }, deps);
};