Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

usePrevious.js 607B

1234567891011121314151617181920212223242526
  1. import { useEffect, useRef } from 'react';
  2. /**
  3. * Store the last of some value. Tracked via a `Ref` only updating it
  4. * after the component renders.
  5. *
  6. * Helpful if you need to compare a prop value to it's previous value during render.
  7. *
  8. * ```ts
  9. * function Component(props) {
  10. * const lastProps = usePrevious(props)
  11. *
  12. * if (lastProps.foo !== props.foo)
  13. * resetValueFromProps(props.foo)
  14. * }
  15. * ```
  16. *
  17. * @param value the value to track
  18. */
  19. export default function usePrevious(value) {
  20. var ref = useRef(null);
  21. useEffect(function () {
  22. ref.current = value;
  23. });
  24. return ref.current;
  25. }