useLayoutEffect.js 917 B

1234567891011121314151617181920212223242526272829
  1. import * as React from 'react';
  2. import canUseDom from "../Dom/canUseDom";
  3. /**
  4. * Wrap `React.useLayoutEffect` which will not throw warning message in test env
  5. */
  6. var useInternalLayoutEffect = process.env.NODE_ENV !== 'test' && canUseDom() ? React.useLayoutEffect : React.useEffect;
  7. var useLayoutEffect = function useLayoutEffect(callback, deps) {
  8. var firstMountRef = React.useRef(true);
  9. useInternalLayoutEffect(function () {
  10. return callback(firstMountRef.current);
  11. }, deps);
  12. // We tell react that first mount has passed
  13. useInternalLayoutEffect(function () {
  14. firstMountRef.current = false;
  15. return function () {
  16. firstMountRef.current = true;
  17. };
  18. }, []);
  19. };
  20. export var useLayoutUpdateEffect = function useLayoutUpdateEffect(callback, deps) {
  21. useLayoutEffect(function (firstMount) {
  22. if (!firstMount) {
  23. return callback();
  24. }
  25. }, deps);
  26. };
  27. export default useLayoutEffect;