[英]Passing image URL to functional component in React native
I want my avatar component to show the avatar with the URL passed to the component.我希望我的头像组件显示带有传递给组件的 URL 的头像。 So if I pass avatar1 it shows avatar1.
因此,如果我通过 avatar1,它会显示 avatar1。 I have tried several methods, but nothing seems to work.
我尝试了几种方法,但似乎没有任何效果。
My avatar component looks like this currently.我的头像组件目前看起来像这样。 I want to get rid of the static URL
我想摆脱 static URL
export default function Avatar() {
return (
<Image source={require("../assets/avatar.png")} style={styles.avatarStyle} />
);
}
const styles = StyleSheet.create({
avatarStyle: {
resizeMode: 'contain',
height: "60%",
alignSelf: "flex-start",
position: "absolute",
bottom: 0
}
});
Probably you trying to pass this avatar1 via props so you need to use the props, passing it to the function可能你试图通过道具传递这个 avatar1 所以你需要使用道具,将它传递给 function
export default function Avatar({avatarUri}) {
return (
<Image source={{uri: avatarUri}} style={styles.avatarStyle} />
);
}
const styles = StyleSheet.create({
avatarStyle: {
resizeMode: 'contain',
height: "60%",
alignSelf: "flex-start",
position: "absolute",
bottom: 0
}
});
https://reactnative.dev/docs/props https://reactnative.dev/docs/props
Make sure the next things:确保接下来的事情:
Image path
check correct path Image path
检查正确路径width
style param to the image, it is required to render.width
样式参数添加到图像中,它是渲染所必需的。resizeMode
it's a prop of Image, not a style param resizeMode
它是 Image 的道具,而不是样式参数Fixed style:固定风格:
const styles = StyleSheet.create({
avatarStyle: {
height: "60%",
width: 150, // FOR EXAMPLE
alignSelf: "flex-start",
position: "absolute",
bottom: 0
}
});
The complete code:完整代码:
export default function Avatar() {
return (
<Image source={require("../assets/avatar.png")} style={styles.avatarStyle} resizeMode='contain'/>
);
}
const styles = StyleSheet.create({
avatarStyle: {
height: "60%",
width: 150, // FOR EXAMPLE
alignSelf: "flex-start",
position: "absolute",
bottom: 0
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.