-
Notifications
You must be signed in to change notification settings - Fork 145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
StrictMode breaks Masonry #183
Comments
Thanks for helping us fix our issue too! We upgraded to React 18 and got the same issue. |
Regarding triggering the layout function on window resize, this is the route I went: Create a hook: import React, {useState, useEffect} from 'react'
const useWindowWidth = () => {
const [windowWidth, setWindowWidth] = useState(undefined);
useEffect(() => {
const handleResize = () => {
setWindowWidth(window.innerWidth);
}
window.addEventListener('resize', handleResize);
handleResize();
// Remove event listener on cleanup
return () => window.removeEventListener('resize', handleResize);
}, []);
return windowWidth;
}
export default useWindowWidth Then in my component which holds masonry, create a ref and add it to the import {useEffect, useRef} from "react";
import Masonry, {MasonryOptions} from 'react-masonry-component';
import useWindowWidth from "hooks/useWindowWidth";
const MasonryGrid = () => {
const masonry = useRef(undefined)
const windowSize = useWindowWidth();
useEffect(() => {
if (masonry.current) {
masonry.current.performLayout()
}
}, [windowSize])
return (
<Masonry ref={masonry} style={{position: 'relative'}}>
{/* This is a grid item */}
<div style={{position: 'absolute'}}></div>
</Masonry>
)
} |
Hello, everyone!
I tried to use 'react-masonry-component' but found very strange behaviour (as for me). Wrapping with
<React.StrictMode>
breaks masonry - it does not set any positioning.Layout with and without StrictMode:
Inline styles with and without StrictMode:
It can be fixed by adding
{ position: relative }
to a parent and{ position: absolute }
to children, but it's a dirty fix: also masonry stops rebuilding on resize, you need to trigger the layout function. Any ideas?Reproduced in Codesandbox
The text was updated successfully, but these errors were encountered: