useMobile.js 656 B

1234567891011121314151617181920
  1. import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
  2. import { useState } from 'react';
  3. import isMobile from "../isMobile";
  4. import useLayoutEffect from "./useLayoutEffect";
  5. /**
  6. * Hook to detect if the user is on a mobile device
  7. * Notice that this hook will only detect the device type in effect, so it will always be false in server side
  8. */
  9. var useMobile = function useMobile() {
  10. var _useState = useState(false),
  11. _useState2 = _slicedToArray(_useState, 2),
  12. mobile = _useState2[0],
  13. setMobile = _useState2[1];
  14. useLayoutEffect(function () {
  15. setMobile(isMobile());
  16. }, []);
  17. return mobile;
  18. };
  19. export default useMobile;