[英]load image as prop in react-native
so I have a customHeader component where I am trying to load images into like so所以我有一个 customHeader 组件,我试图在其中加载图像
<Image source={props.logoImg} style={styles.icon} />
and on the page I'll pass in the prop like so在页面上我会像这样传递道具
<CustomHeader
navigation={props.navigation} //call props.navigation in component
primaryColor={primaryColor}
coName={coName}
logoImg={require('../content/...some image...'}
/>
This way it doesn't throw an error, but it doesn't load.这样它不会抛出错误,但不会加载。 I know it has something to do with passing it as a prop because I can load images straight into the screens.
我知道这与将其作为道具传递有关,因为我可以将图像直接加载到屏幕中。 I'm just not sure what I'm doing wrong or what I'm missing here.
我只是不确定我做错了什么或我在这里错过了什么。
I have tried using the require inside of the component, but that throws an error at compile because react-native needs the images to be "required" at compile.我曾尝试在组件内部使用 require ,但这会在编译时引发错误,因为 react-native 需要在编译时“需要”图像。
this should work这应该有效
in the Wrapping component在包装组件中
<CustomHeader
imageUri={'../content/...some image...'}
/>
and then in the inner component然后在内部组件中
<Image source={require(props.imageUri)} />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.