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:
AsadUmar 2022-02-17 00:08:57 +05:00 committed by GitHub
parent b3c4ce05fa
commit 50423111af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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;
} }