react native auto height image code example
Example 1: react native asign width to image
import { Dimensions } from "react-native";
const win = Dimensions.get('window');
<Image
style={{
width: win.width/2,
height: win.width/2,
resizeMode: "contain",
alignSelf: "center",
borderWidth: 1,
borderRadius: 20,
}}
source={{uri:'https://facebook.github.io/react/img/logo_og.png'}}
resizeMode="stretch"
/>
Example 2: relative width and height image react native
const dimensions = Dimensions.get('window');
const imageHeight = Math.round(dimensions.width * 9 / 16);
const imageWidth = dimensions.width;
return (
<Image
style={{ height: imageHeight, width: imageWidth }}
/>
);
Example 3: react native asign width to image
First import Dimensions from react-native
import { Dimensions } from 'react-native';
then you have to get the dimensions of the window
const win = Dimensions.get('window');
Now calculate ratio as
const ratio = win.width/541;
now the add style to your image as
imageStyle: {
width: win.width,
height: 362 * ratio,
}