useLayoutEffect.js 973 B

1234567891011121314151617181920212223242526272829
  1. /**
  2. * copy from https://github.com/react-component/util/blob/9d5cb8946da29e690bead78b2c251da6f7cbd0eb/src/hooks/useLayoutEffect.ts
  3. */
  4. import * as React from 'functional-mini/compat';
  5. /**
  6. * Wrap `React.useLayoutEffect` which will not throw warning message in test env
  7. */
  8. var useInternalLayoutEffect = React.useEffect;
  9. var useLayoutEffect = function (callback, deps) {
  10. var firstMountRef = React.useRef(true);
  11. useInternalLayoutEffect(function () {
  12. return callback(firstMountRef.current);
  13. }, deps);
  14. // We tell react that first mount has passed
  15. useInternalLayoutEffect(function () {
  16. firstMountRef.current = false;
  17. return function () {
  18. firstMountRef.current = true;
  19. };
  20. }, []);
  21. };
  22. export var useComponentUpdateEffect = function (callback, deps) {
  23. useLayoutEffect(function (firstMount) {
  24. if (!firstMount) {
  25. return callback();
  26. }
  27. }, deps);
  28. };
  29. export default useLayoutEffect;