Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

35 wiersze
815 B

  1. import { useEffect, useRef } from 'react';
  2. /**
  3. * Runs an effect only when the dependencies have changed, skipping the
  4. * initial "on mount" run. Caution, if the dependency list never changes,
  5. * the effect is **never run**
  6. *
  7. * ```ts
  8. * const ref = useRef<HTMLInput>(null);
  9. *
  10. * // focuses an element only if the focus changes, and not on mount
  11. * useUpdateEffect(() => {
  12. * const element = ref.current?.children[focusedIdx] as HTMLElement
  13. *
  14. * element?.focus()
  15. *
  16. * }, [focusedIndex])
  17. * ```
  18. * @param effect An effect to run on mount
  19. *
  20. * @category effects
  21. */
  22. function useUpdateEffect(fn, deps) {
  23. var isFirst = useRef(true);
  24. useEffect(function () {
  25. if (isFirst.current) {
  26. isFirst.current = false;
  27. return;
  28. }
  29. return fn();
  30. }, deps);
  31. }
  32. export default useUpdateEffect;