0

I have the following array of objects.

{
  "_id": {
    "$oid": "5f15d92ee520d44421ed8e9b"
  },
  "image": "",
  "brand": "IKEA",
  "price": 350.9,
  "rating": 5,
  "numReviews": 0,
  "isFeatured": true,
  "name": "Garden Chair",
  "description": "beautiful chair for garden",
  "category": {
    "$oid": "5f15d5b7cb4a6642bddc0fe8"
  },
  "countInStock": 10,
  "__v": 0
},{
  "_id": {
    "$oid": "5f15d964e520d44421ed8e9c"
  },
  "image": "",
  "brand": "OBI",
  "price": 1350.9,
  "rating": 5,
  "numReviews": 0,
  "isFeatured": true,
  "name": "Swimming Pool",
  "description": "beautiful Swimming Pool for garden",
  "category": {
    "$oid": "5f15d5b7cb4a6642bddc0fe8"
  },
  "countInStock": 10,
  "__v": 0

I tried to pass it to a component called ProductCard in the following way.

From ProductContainer -> <FlatList <ProductList /> /> -> ProductCard. 

I can see the objects inside ProductList using console.log(), but I cannot render (or print in console) the item.price inside ProductCard.

ProductContainer.js

const data = require("../../assets/data/products.json");
...

const ProductContainer = () => {
  ...

  return (
    <View>
      <Text>Product Container</Text>
      <View style={{ marginTop: 100 }}>
        <FlatList
          data={products}
          renderItem={({ item }) => <ProductList key={item.id} item={item} />}
          keyExtractor={(item) => item.name}
          numColumns={2}
        />
      </View>
    </View>
  );
};

export default ProductContainer;

And then to ProductList.js

import ProductCard from "./ProductCard";

var { width } = Dimensions.get("window");

const ProductList = (props) => {
  const { items } = props;
  console.log(items.price);
  <TouchableOpacity style={{ width: "50%" }}>
    <View style={{ width: width / 2, backgroundColor: "gainsboro" }}>
      <ProductCard {...item} />
    </View>
  </TouchableOpacity>;
};

export default ProductList;

and finally, ProductCard.js

const ProductCard = ({ props }) => {
  // console.log(props);
  const item = props;

  return (
    <View>
      <Text>{props.price}</Text>
    </View>
  );
};

Please help. Thanks.

I have tried to debug using console.log(). See above for the details.

1
  • Item seems to be undeclared in <ProductCard {...item} /> Commented Dec 31, 2022 at 3:27

2 Answers 2

1

There is a typo: item vs items.

<ProductList key={item.id} item={item} />

const ProductList = (props) => {
  const { items } = props;
...
};
Sign up to request clarification or add additional context in comments.

Comments

0

You should be passing items as a property in ProductList:

import ProductCard from "./ProductCard";

var { width } = Dimensions.get("window");

const ProductList = (props) => {
  const { items } = props;
  console.log(items.price);
  <TouchableOpacity style={{ width: "50%" }}>
    <View style={{ width: width / 2, backgroundColor: "gainsboro" }}>
      <ProductCard item={item} />
    </View>
  </TouchableOpacity>;
};

export default ProductList;

{...item} would distribute each property of item into its own prop, e.g. it's equivalent of

image={""} brand={"IKEA"} price={350.9} rating={5} ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.