Fix/use has window width changed enough (#894)
* fix render of toggle. * Fix useHasWindowWidthChangedEnough * fix flow Co-authored-by: Asad Umar <au@visuary.fr>
This commit is contained in:
parent
b3c4ce05fa
commit
50423111af
1 changed files with 4 additions and 4 deletions
|
@ -24,14 +24,14 @@ export function useWindowSize() {
|
||||||
|
|
||||||
function useHasWindowWidthChangedEnough(comparisonFn: (windowSize: number) => boolean) {
|
function useHasWindowWidthChangedEnough(comparisonFn: (windowSize: number) => boolean) {
|
||||||
const isWindowClient = typeof window === 'object';
|
const isWindowClient = typeof window === 'object';
|
||||||
const initialState = isWindowClient ? comparisonFn(window.innerWidth) : comparisonFn(DEFAULT_SCREEN_SIZE);
|
const initialState: boolean = isWindowClient ? comparisonFn(window.innerWidth) : comparisonFn(DEFAULT_SCREEN_SIZE);
|
||||||
const [windowSize, setWindowSize] = React.useState(initialState);
|
const [windowSize, setWindowSize] = React.useState<boolean>(initialState);
|
||||||
const prev = useRef(window.innerWidth);
|
const prev = useRef<boolean>(initialState);
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
function setSize() {
|
function setSize() {
|
||||||
const curr = comparisonFn(window.innerWidth);
|
const curr = comparisonFn(window.innerWidth);
|
||||||
if (prev !== curr) {
|
if (prev.current !== curr) {
|
||||||
setWindowSize(curr);
|
setWindowSize(curr);
|
||||||
prev.current = curr;
|
prev.current = curr;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue