0

当我尝试获取 js 对象中存储为字符串的图像路径时出现此错误,其中 require() 作为道具。

Warning: Failed prop type: Invalid prop `source` supplied to `Image`.

CategoriesScreen.js

import React from "react";
import {
  View,
  Text,
  FlatList,
  StyleSheet,
  TouchableOpacity,
} from "react-native";

import { CATEGORIES } from "../Data/Dummy-Data";
import CategoryGrid from "../component/categoryGrid";

const CategoriesScreen = (props) => {
  const renderGridItem = (itemData) => {
    return (
      <CategoryGrid
        title={itemData.item.title}
        imgUrl={itemData.item.url}
//console.log (itemData.item.url)-> require(..) ->image path


        onSelect={() =>
          props.navigation.navigate({
            routeName: "CategoryMeals",
            params: {
              categoryId: itemData.item.id,
            },
          })
        }
      ></CategoryGrid>
    );
  };
  return (
    <View>
      <FlatList data={CATEGORIES} renderItem={renderGridItem} numColumns={2} />
    </View>
  );
};
CategoriesScreen.navigationOptions = {
  headerTitle: "Meals Categories",
};
const styles = StyleSheet.create({
  screen: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    //this is unused till now.
  },
});

export default CategoriesScreen;

这里我存储了所有图片的路径

虚拟数据.js

import Category from "../Models/category";

export const CATEGORIES = [
  new Category("C1", "Italian", "require('../assets/Images/italian.jpeg')"),
  new Category("C2", "Indian", "require('../assets/Images/Indian.jpeg')"),
  new Category("C3", "French", "require('../assets/Images/French.jpeg')"),
  new Category(
    "C4",
    "Hamburgers",
    "require('../assets/Images/Hamburgers.jpeg')"
  ),
  new Category("C5", "German", "require('../assets/Images/German.jpeg')"),
  new Category("C6", "Asian", "require('../assets/Images/Asian.jpeg')"),
  new Category("C7", "Breakfast", "require('../assets/Images/Breakfast.jpeg')"),
  new Category("C8", "Dinner", "require('../assets/Images/Dinner.jpeg')"),
];

类别.js

class Category {
  constructor(id, title, url) {
    this.id = id;
    this.title = title;
    this.url = url;
  }
}
export default Category;

CategoryGrid.js

import React from "react";
import {
  TouchableOpacity,
  StyleSheet,
  View,
  Text,
  ImageBackground,
} from "react-native";

const CategoryGrid = (props) => {
  return (
    <TouchableOpacity style={styles.gridItems} onPress={props.onSelect}>
      <View>
        <ImageBackground
          source={props.imgUrl}
          style={{ width: "100%", height: "90%" }}
        />
        <Text>{props.title}</Text>
      </View>
    </TouchableOpacity>
  );
};

const styles = StyleSheet.create({
  gridItems: {
    flex: 1,
    margin: 15,
    height: 150,
  },
});

export default CategoryGrid;

console.log() 在 CategoriesScreen.js 和 categoryGrid.js -> require(../assets/Images/image.jpeg) 中说

我正在尝试将图像路径作为从 CategoryScreen.js 到 categoryGrid.js 的道具获取,该道具存储为 Dummy-Data.js 中的对象。

知道我做错了什么吗?

4

2 回答 2

2

像这样做

<ImageBackground
  source={{uri: props.imgUrl}}
  style={{ width: "100%", height: "90%" }}
/>
于 2021-05-11T09:27:07.723 回答
0
import Category from "../Models/category";

export const CATEGORIES = [
  new Category("C1", "Italian", require("../assets/Images/italian.jpeg")),
  new Category("C2", "Indian", require("../assets/Images/Indian.jpeg")),
  new Category("C3", "French", require("../assets/Images/French.jpeg")),
  new Category("C4", "Hamburgers", require("../assets/Images/Hamburgers.jpeg")),
  new Category("C5", "German", require("../assets/Images/German.jpeg")),
  new Category("C6", "Asian", require("../assets/Images/Asian.jpeg")),
  new Category("C7", "Breakfast", require("../assets/Images/Breakfast.jpeg")),
  new Category("C8", "Dinner", require("../assets/Images/Dinner.jpeg")),
];

于 2021-05-11T09:41:14.763 回答