JavaScript Change value in react js on window resize change value react,change value react-select,change props value

I'm building a photo gallery in react js. Obviously it's going to have to be responsive and I've tackled this by setting values like so in the component's render method:

let thumbWidth = window.innerWidth >= 480 ? 75 : 100;

The problem is that I need the const value to change when the window is resized. My my first attempts was to build a function like so and bind it in the constructor:

getThumbWidth = (vpWidth) => {
    if(vpWidth >= 480 ) {
        this.setState({thumbSize: 120}); 
     } else {
        this.setState({thumbSize: 50}); 
     }
}

This worked in terms of setting the variable's initial value but how do I trigger the function when the window resizes?

Have tried adding a resize function:

resize = () => this.getThumbWidth()

...and then changing getThumbWidth so that it sets the value in the component's sate - the idea being that when state is updated the component automatically rerenders:

getThumbWidth = (vpWidth) => {
    if(vpWidth >= 480 ) {
        this.setState({thumbSize: 120}); 
     } else {
        this.setState({thumbSize: 50}); 
     }
}

And then calling it via lifecycle methods as suggested in several other solutions but the event listener does not appear to be firing:

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

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

Still not working though... any ideas?

Answer:1

U can add ref attribute to container of your component:

return (
    <div
       className={mainClass}
       ref={(elem) => { this.elem = elem; }}
    >
        your gallery here
   </div>

Then in your componentDidMount u have access to all events connected to your container. Then u can invoke your function depending on changes on these events. For example:

componentDidMount() {
   const parent = this.elem.parentNode;
       if (parent.scrollHeight > parent.clientHeight) {
         parent.onscroll = (() => this.yourMethod(parent));
       } 
}
Answer:2

We have URL when we hit URL URL in browser we got data. We got Data like [{"UserId":"c2fbd9fb-a423-4d33-9ea4-3aa58f7b52cf","UserType":"Parent","OutPutMessage":"Sucess"}]. But We need get data ...

We have URL when we hit URL URL in browser we got data. We got Data like [{"UserId":"c2fbd9fb-a423-4d33-9ea4-3aa58f7b52cf","UserType":"Parent","OutPutMessage":"Sucess"}]. But We need get data ...

I want to access the developer tools on this domain "http://umang.gov.in" using F12 or right click of the mouse but the site website developer block this option. I follow this step to access this. ...

I want to access the developer tools on this domain "http://umang.gov.in" using F12 or right click of the mouse but the site website developer block this option. I follow this step to access this. ...

  1. how to unblock developer tools on chromebook

I am developing an Ember-cli app with ember 2.1. I created a templates/loading.hbs and several other templates. My foo template has a input that I use to send a queryParam to foo/bar. foo/bar uses the ...

I am developing an Ember-cli app with ember 2.1. I created a templates/loading.hbs and several other templates. My foo template has a input that I use to send a queryParam to foo/bar. foo/bar uses the ...

I'm using rooms to send a 'private' message to a client and it works however the message being sent is duplicated by the number of clients I have and I believe this is because of my .on('message') ...

I'm using rooms to send a 'private' message to a client and it works however the message being sent is duplicated by the number of clients I have and I believe this is because of my .on('message') ...