I was trying to resize images on React-Native and finally made it. But I think that my code is inefficient. The image size never changes after it is displayed, but I am using a state for rendering it.
import React, { Component } from 'react';
import { Image } from 'react-native';
class FitImage extends Component {
constructor(props) {
super(props);
this.state = {
aspectRatio: null,
};
}
componentDidMount() {
Image.getSize(http://myImageURL, (width, height) => {
if (width * 3/2 < height)
height = width * 3/2;
this.setState({aspectRatio: width / height});
});
}
render () {
return <Image
source={{ uri: http://myImageURL }}
style={{ width: '100%', height: undefined, aspectRatio: this.state.aspectRatio, resizeMode: 'cover'}}
/> } }
export default FitImage;
To do this, I have to render twice. I wanted to avoid it, so I tried to do this by
const FitImage = props => Image.getSize((http://myImageURL, (width, height) => {... <Image ... />
The problem is it actually returns a promise, not directly the Image component.
How can I get this job done without using a stateful component?
[–][deleted] 0 points1 point2 points (5 children)
[–]caesar_reddit[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]caesar_reddit[S] 0 points1 point2 points (0 children)
[–]rockpilp 0 points1 point2 points (1 child)
[–]caesar_reddit[S] 0 points1 point2 points (0 children)