Code Situation
I have a simple react app setup. The home component should be a image gallery with a masonry layout. I found this library: Bricks.js I load the data (name, date, url to image) of the items from my api with fetch.
Here are some parts of my code in Home.js:
The constructor()
constructor() {
super();
this.state = {
galleryItems: []
};
this.instance = {}
}
This function loads the data of the items.
getItems(limit){
fetch('http://localhost:3000/api/posts/next/' + limit)
.then((response) => {
return response.json()
}).then((data) => {
this.setState({galleryItems: data});
})
}
I used the componentDidMount() function to load 5 items and create the Bricks.js instance.
componentDidMount(){
this.getItems(5)
//sizes for Brick.js
const sizes = [
{ columns: 5, gutter: 3 },
{ mq: '768px', columns: 2, gutter: 3 },
{ mq: '1024px', columns: 3, gutter: 3 }
]
//init instance
this.instance = Bricks({
container: '.gallery',
packed: 'packed',
sizes: sizes
})
this.instance.resize(true); //<-adds a resize event listener
if (this.state.galleryItems.length > 0) {
this.instance.pack() //<- This should create the masonry layout
}
}
And for loading more image I wrote this in the componentDidUpdate() function.
componentDidUpdate(){
if (this.state.galleryItems.length > 0) {
return this.instance.pack()
}
else{
return this.instance.update() //<- updates the layout
}
}
The render() function converts the data from the server to a <Item>
which is just another component that creates a <img>
element
render() {
const items = this.state.galleryItems.map((item, _id) => {
return <Item key={_id} url={this.state.url + item.url}></Item>
})
return (
<div>
Home Component
<div className="gallery">
{items}
</div>
</div>
);
}
Problem
If I open my app in firefox it works fine. But in chrome the images are just on top of each other. If I resize the window the masonry layout is created fine. I seems chrome is either too fast or slow. What is wrong with my code that this can happen?