I am getting this error when I am trying to get the image path which is in the js object stored as a string with require() as a prop.

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;

Here I have stored the path of all images

Dummy-data.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')"),
];

category.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() says in CategoriesScreen.js and in categoryGrid.js -> require(../assests/Images/image.jpeg)

I am trying to get the image path as a prop from CategoryScreen.js to categoryGrid.js which is stored as an object in Dummy-Data.js.

Any idea what I am doing wrong?

2

There are 2 answers

1
Kartikey On BEST ANSWER
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")),
];

2
Sahil Ardeshna On

do it like this

<ImageBackground
  source={{uri: props.imgUrl}}
  style={{ width: "100%", height: "90%" }}
/>