Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. import {useState, useEffect} from 'react';
  2. import throttle from 'lodash.throttle';
  3.  
  4. const getDeviceConfig = (width) => {
  5. if(width < 320) {
  6. return 'xs';
  7. } else if(width >= 320 && width < 720 ) {
  8. return 'sm';
  9. } else if(width >= 720 && width < 1024) {
  10. return 'md';
  11. } else if(width >= 1024) {
  12. return 'lg';
  13. }
  14. };
  15.  
  16. const useBreakpoint = () => {
  17. const [brkPnt, setBrkPnt] = useState(() => getDeviceConfig(window.innerWidth));
  18.  
  19. useEffect(() => {
  20. const calcInnerWidth = throttle(function() {
  21. setBrkPnt(getDeviceConfig(window.innerWidth))
  22. }, 200);
  23. window.addEventListener('resize', calcInnerWidth);
  24. return () => window.removeEventListener('resize', calcInnerWidth);
  25. }, []);
  26.  
  27. return brkPnt;
  28. }
  29. export default useBreakpoint;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement