提供给Image的React-Native Image无效的prop'source'

提供给Image的React-Native Image无效的prop'source',第1张

提供给Image的React-Native Image无效的prop'source'

这不是推荐的动态分配图像的方法,因为在编译包之前,React Native必须知道所有图像源。

根据文档,这是一个如何动态加载图像的示例:

// GOOD<Image source={require('./my-icon.png')} />;// BADvar icon = this.props.active ? 'my-icon-active' : 'my-icon-inactive';<Image source={require('./' + icon + '.png')} />;// GOODvar icon = this.props.active  ? require('./my-icon-active.png')  : require('./my-icon-inactive.png');<Image source={icon} />;

https://facebook.github.io/react-
native/docs/images.html

希望能帮助到你

编辑:如果您知道所有可以加载的图像,则可以尝试如下 *** 作:

// Create a file containing the references for your images// images.jsconst images = {  logo: {    uri: require('your-image-path/logo.png')  },  banner: {     uri: require('your-image-path/banner.png')  }}export { images };//YourComponent.jsimport { images } from 'yourImagesPath';// for this test, expected to return [ { name: logo }, { name: banner} ]const imagesFromTheServer = (your fetch);imagesFromTheServer.map(image => {  if (!images[image]) {    return <Text>Image not found</Text>;  }  return <Image source={images[image].uri} />; // if image = logo, it will return images[logo] containing the require path as `uri` key});

这很hacky,但可能会起作用。

让我知道是否有帮助



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5622584.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存