drewvy22
drewvy22

Reputation: 225

Setting Image to size relative to screen size?

I'm very new to React-Native and Javascript and have been trying to learn. I'm trying to set an image's width to the screen size divided by 10. I set up a constructor and a state with the screen width and height, but am unable to use the state to set the width or height of my image.

constructor(props) {
     super(props);
     const { width, height } = Dimensions.get("window")
     this.state = {
       width,
       height
     }
}

If I do this...

<Image source={require('./my-icon.png')} style={{width: {this.state.width}, height: 40}}/>

It gives me an error saying that 'this' is a reserved word.

Upvotes: 1

Views: 200

Answers (1)

drewvy22
drewvy22

Reputation: 225

I fixed it. I guess I made a dumb mistake. I removed the brackets and it worked.

<Image source={require('./my-icon.png')} style={{width: this.state.width/10, height: 40}}/>

Upvotes: 1

Related Questions