XeNote/lib/hooks.js

25 lines
745 B
JavaScript
Raw Normal View History

2020-11-28 15:45:01 +00:00
import React, { useState, useEffect, useRef } from "react";
export function useWindowWidth() {
const [screenSize, setScreenSize] = useState(window.innerWidth)// S | M | L
const screenListener = () => {
const currentSize = window.innerWidth;
//if size (not width) is changed, then change state
if (screenSize != currentSize) {
setScreenSize(currentSize);
}
}
useEffect(() => {
// Once screenSize changed this will be fired
window.addEventListener("resize", screenListener);
// for removing repeatedly rendering
return () => {
window.removeEventListener("resize", screenListener);
}
})
return screenSize
}