S
S
sergeyviktorovich2021-11-07 14:40:01
React
sergeyviktorovich, 2021-11-07 14:40:01

How to work with the resize event in react and how to determine the screen width and change the state when loading?

this.state = {
            perpage: 24,
        }


at 768 I need to reduce the number of cards in perpage: 12 and do the same if the screen is 768 when rendering

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-11-07
@0xD34F

onResize = () => {
  this.setState(() => ({
    /* ... */
  }));
}

componentDidMount() {
  window.addEventListener('resize', this.onResize);
  this.onResize();
}

componentWillUnmount() {
  window.removeEventListener('resize', this.onResize);
}

https://jsfiddle.net/jmz39vwo/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question