I have created a grid with some images and want to update the number of columns on certain widths. For example:
- width > 960px: 3 columns
- width > 660px: 2 columns
- width < 660px: 1 column
I've created a function to update the number of rows, but how can I rerender the grid without pressing F5?
My states:
this.state = {
numberOfColumns: 3,
breakpointSm: 660px,
breakpointMd: 960px,
};
My function:
getNumberOfColumns(){
let smallDevices = window.matchMedia( "(max-width: " + this.state.breakpointSm + ")" );
let mediumDevices = window.matchMedia( "(max-width:" + this.state.breakpointMd + ")" );
let columns;
if (smallDevices.matches) {
columns = 1;
} else if (mediumDevices.matches){
columns = 2;
} else {
columns = 3;
}
this.setState({
numberOfColumns: columns
});
}
Component will mount:
componentWillMount() {
this.getNumberOfColumns();
}