【问题标题】:React-Native Image Invalid prop 'source' supplied to ImageReact-Native Image 提供给 Image 的无效道具“源”
【发布时间】:2018-10-24 08:17:02
【问题描述】:

我认为这是一个非常烦人的错误,感觉没有解决方案,但我想分享和询问.. 我从服务器获取数据并从那里获取图像源,并在我的移动反应中使用相同的图像路径-原生应用。

我像这样从服务器获取数据:

$id = $request->userid;
$items = DB::table('item_user')->where('user_id', $id)->latest()->get();

$new = new Collection();

foreach($items as $item){

$new[] = array(
   ... ,
   'picturePath' => 'require'."('./". $item->picturePath ."')"
);

}

return $new;

在前端我尝试渲染并且我在本地拥有这些图像。所以当我在本地使用它时:

需要('./images/...')

它有效.. 但像这样它不起作用:

_renderItem = ({item}) => {

        return(
            <View>
               <Image source={ item.picturePath } style={{width: 15, height: 15}}/>
            </View>
        );

    };


    render(){
        return(

        <View>
           <FlatList
                data={this.state.items}
                renderItem={this._renderItem}
                keyExtractor={this._keyExtractor}
            />
         </View>
        );
    }

我得到error,我该如何解决这个问题:

警告:道具类型失败:提供给“图像”的道具“来源”无效。

【问题讨论】:

  • react-native 不能在运行时需要静态资源。它必须在构建时。
  • 是您的 item.picturePath 包含 http 的完整路径吗?
  • 不,它像 require('./images/.../..') 并且我在本地拥有这些图像。因此,当我像 require('./images/...') 一样在本地使用它时,它可以工作..

标签: reactjs react-native


【解决方案1】:

作为React Native Documentation says,在编译你的包之前需要加载你所有的图像源。

在 React Native 中添加一个简单的图像应该是这样的:

<Image source={require('./path_to_your_image.png')} />

假设你有这个:

const slides = {
  car: './car.png',
  phone: '../phone.png',
}

然后您将slides 作为参数传递,但您仍然无法像这样使用它(即使在逻辑上应该有效):

<Image source={require(props.car)} />

sildes{}中使用require()

const slides = {
  car: require('./car.png'),
  phone: require('../phone.png'),
}

然后像这样使用它:

<Image source={props.car} />

【讨论】:

    【解决方案2】:

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

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

    // GOOD
    <Image source={require('./my-icon.png')} />;
    
    // BAD
    var icon = this.props.active ? 'my-icon-active' : 'my-icon-inactive';
    <Image source={require('./' + icon + '.png')} />;
    
    // GOOD
    var 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.js
    const images = {
      logo: {
        uri: require('your-image-path/logo.png')
      },
      banner: { 
        uri: require('your-image-path/banner.png')
      }
    }
    
    export { images }; 
    
    
    //YourComponent.js
    import { 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,但可能会起作用。

    如果有帮助请告诉我

    【讨论】:

    • 我知道这一点,但是我将如何从服务器获取图像路径..?以及我将如何渲染数百张图像?
    • 您必须映射所有图像并使用服务器中的条件来加载它们。给我5分钟,我会用一个例子来更新答案
    • ok 这将是完美的,因为实际上一切都很好。我的意思是这个路径是一个字符串,因为它在 require('...') 中使用,但它不起作用.. 我正在等待谢谢!
    • 我更新了答案。请注意,这只是表达这个想法的一个例子。希望对你有帮助
    • 当您知道要加载图像时,拥有一组所需图像是一个不错的设计。
    猜你喜欢
    • 2019-08-05
    • 2020-12-21
    • 1970-01-01
    • 2021-10-20
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多