You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importReactfrom'react'import{View}from'react-native'functionMyComponent(){constonLayout=(event)=>{const{x, y, height, width}=event.nativeEvent.layout;}return(<ViewonLayout={onLayout}><OtherComponent/></View>)}
Usage example
importReact,{useState}from"react";import{Image,View,ScrollView,Text}from"react-native";constmyImage=require("../assets/images/SomeLocalImage.png");constmyImageDetails=Image.resolveAssetSource(myImage);/** * Goal: render image as large as possible but without causing overflow * * Trying with local image */exportdefaultfunctionImageTest(){const[multiplier,setMultiplier]=useState(1);return(<Viewstyle={{flex: 1,paddingHorizontal: 16}}><ScrollView><Text>Image trial</Text><View// using layout to avoid hardcode padding calculation// just using this for getting `width`onLayout={(event)=>{const{ width }=event.nativeEvent.layout;if(multiplier===1)setMultiplier(width/myImageDetails.width);}}/><Imagesource={myImage}style={{height: myImageDetails.height*multiplier,width: myImageDetails.width*multiplier,}}/><View><Imagesource={myImage}style={{width: "100%",borderWidth: 1,}}resizeMode="contain"/></View></ScrollView></View>);}
The text was updated successfully, but these errors were encountered:
sanjarcode
changed the title
Get dimensions imperatively - RN local image, View
Get dimensions imperatively - RN Image, View
Dec 8, 2023
The
Dimensions
API is present but useful due to padding etc. Pretty non informative.Image
https://stackoverflow.com/a/59801268
for network images - there's
Image.getSize
View width height
https://stackoverflow.com/a/66870056
Usage example
The text was updated successfully, but these errors were encountered: