简体   繁体   English

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

[英]Warning: Failed prop type: Invalid prop `source` supplied to `Image`

I have this warning in my project: "Warning: Failed prop type: Invalid prop source supplied to Image ". 我的项目中有以下警告:“警告:道具类型失败:提供给Image道具source无效”。 I'm loading an image from sampleProducts.json to Recommendend.js and pass it as props in Products.js, but the image won't load because of the warning. 我正在将图像从sampleProducts.json加载到Recommendend.js并将其作为props传递给Products.js,但是由于警告,该图像将无法加载。

sampleProducts.json sampleProducts.json

"recommended": {
    "product1": {
        "id": "1",
        "image": "require('../images/sample1.png')",
        "name": "Sample Product",
    },

Recommmended.js Recommmended.js

{Object.values(recommended).map(product => (
                    <Products
                        key={ product.id }
                        productImage={ product.image }
                        productName={ product.name }
                    />
                ))}

Products.js Products.js

<Card style={ styles.card }>
                    <CardItem>
                        <Image style={ styles.productImage } source={ productImage} resizeMode='contain' />
                    </CardItem>
                    <CardItem>
                        <Body>
                            <Text style={ styles.productName }>{productName}</Text>
                        </Body>
                    </CardItem>
                </Card>

Get rid of the require() on recommended.product1.image and make it just "../images/sample1.jpg" 摆脱对recommended.product1.imagerequire()并将其变成“ ../images/sample1.jpg”

Change your Image component's source prop to be source={require(`${recommended.product1.image}`)} 将您的Image组件的源道具更改为source={require(`${recommended.product1.image}`)}

You cannot use require in JSON format and require not support dynamic path. 您不能使用JSON格式的require ,并且require不支持动态路径。

Can you use switch case statement instead ? 您可以改用switch case语句吗?

function getImage(img_name) {
  switch(img_name) {
    case "sample1.png": return require("../images/sample1.png");
    case "sample2.png": return require("../images/sample2.png");
  }
}

and change JSON to 并将JSON更改为

"recommended": {
    "product1": {
        "id": "1",
        "image": "sample1.png",
        "name": "Sample Product",
    }
}

and use with component below 并与下面的组件一起使用

<Image source={getImage(productImage)} />

If you don't want to use switch case check this answers for other methods 如果您不想使用开关盒,请检查其他方法的答案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:道具类型失败:提供给“图像”的道具“来源”无效。 我在 object 中使用 require() 保存了图像的路径 - Warning: Failed prop type: Invalid prop `source` supplied to `Image`. I have saved path of the image with require() in an object 警告:道具类型失败:提供给“图像”的道具“来源”无效。 在本机反应 - warning: Failed prop type: Invalid prop `source` supplied to `Image`. in react native 警告:道具类型失败:提供给“路线”的道具属性无效。 在途中 - Warning: Failed prop type: Invalid prop `component` supplied to `Route`. in Route 警告:道具类型失败:提供给 `ForwardRef(Slider)` 的道具 `value` 无效 - Warning: Failed prop type: Invalid prop `value` supplied to `ForwardRef(Slider)` 警告:道具类型失败:提供给“ListItem”的道具“副标题”无效 - Warning: Failed prop type: Invalid prop `subtitle` supplied to `ListItem` 警告:道具类型失败:提供给“Form(AddComment)”的道具“initialValues”无效 - Warning: Failed prop type: Invalid prop `initialValues` supplied to `Form(AddComment)` 警告:道具类型失败:提供给视图的道具&#39;backgroundcolor&#39;无效: - warning: failed prop type: invalid prop 'backgroundcolor' supplied to view: %s 类型失败:%s%s,道具,提供给“图像”的无效道具“来源”,应为 [数字] 类型之一 - Failed %s type: %s%s, prop, Invalid prop `source` supplied to `Image`, expected one of type [number] 开玩笑:道具类型失败:提供给“图像”的道具“来源”无效,应为 [数字] 类型之一 - Jest: Failed prop type: Invalid prop `source` supplied to `Image`, expected one of type [number] 为什么图像不显示在 React Native 中? 失败的道具类型:提供给`Image` 的道具`source` 无效 - Why aren't images showing in React Native? Failed prop type: Invalid prop `source` supplied to `Image`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM