简体   繁体   English

如何在 React 中访问儿童道具

[英]how to access children props in React

Really sorry, I know this has been posted before but I just didn't understand how the answers related to my problem.真的很抱歉,我知道这已经发布过,但我只是不明白答案与我的问题有什么关系。 I'm very new to react and need a bit more help than I could find.我对反应很陌生,需要比我能找到的更多的帮助。

My app project has reviews like this;我的应用项目有这样的评论;

const restaurantData = [
  {
    id: 1,
    restaurantName: "Restaurant 1",
    address: "4, Dolphin Way, Swansea SA10 5BZ",
    lat: 48.8737815,
    long: 2.3501649,
    ratings: [{ ratingID: 1, stars: 2, comment: "Terrible service, Yikes!" }],
  },
];

And I have accessed the data no problem like this;而且我已经访问了这样的数据没有问题;

function RestaurantItem({ restaurant }) {
  return (
    <div className="restaurantItem" id={restaurant.id}>
      <h2 className="AsideHeader">{restaurant.restaurantName}</h2>
      <p className="AsideAddress">{restaurant.address} </p>
    </div>
  );
}

I would basically like to access the review data specifically by doing something like this;我基本上想通过这样做来专门访问评论数据;

<div>
  <p>{restaurant.ratings.stars} STAR : </p> {restaurant.ratings.comment}
</div>

But I am not having any luck.但我没有任何运气。 Can someone explain what I am doing wrong and how to address it?有人可以解释我做错了什么以及如何解决吗? Or even what else I would call this to look up the solution?或者甚至我会称之为什么来查找解决方案?

Thank You !谢谢你 !

Since restaurant.ratings is a list you can't display it just like a string.由于 restaurant.ratings 是一个列表,因此您不能像字符串一样显示它。

You could display them like this:你可以像这样显示它们:

<div>
  {restaurant.ratings.map((rating, index) => (
    <p key={"rating-" + rating.ratingID}>
      {rating.stars + " STAR : " + reating.comment}
    </p>
  ))}
</div>

The map method of the list iterates over every element and returns each as "described" by the anonymous method as a JSX Element.列表的 map 方法迭代每个元素,并将每个元素作为 JSX 元素作为匿名方法“描述”返回。

ratings is an array, so you can't access the data as ratings.stars or ratings.comment . ratings是一个数组,因此您不能以ratings.starsratings.comment的形式访问数据。

An approach would be to use map to iterate through the ratings and display all of them for that specific restaurant.一种方法是使用map迭代评级并显示该特定餐厅的所有评级。

<div>
  {restaurant.ratings.map((rating) => (
    <p key={rating.ratingID}>{`${rating.stars} STAR: ${rating.comment}`}</p>
  ))}
</div>

Inside your const restaurantData the ratings is an array.在您的const restaurantData中, ratings是一个数组。 If there is only 1 rating then remove the array: ratings: { ratingID: 1, stars: 2, comment: "Terrible service, Yikes!" }如果只有 1 个评分,则删除数组: ratings: { ratingID: 1, stars: 2, comment: "Terrible service, Yikes!" } ratings: { ratingID: 1, stars: 2, comment: "Terrible service, Yikes!" }

If there will be multiple ratings then use .map to loop every single one.如果会有多个评级,则使用.map循环每个评级。

  {restaurant.ratings.map((rating) => (
    <div key={rating.ratingID}>
      <p>{rating.stars} STAR : </p> {rating.comment}
    </div>
  ))}

You call props on array, but you need call prop on element of array.您在数组上调用道具,但您需要在数组元素上调用道具。

If the ratings property always has one element, then you can write it like this, but it will be a crutch如果 ratings 属性总是有一个元素,那么你可以这样写,但这将是一个拐杖

function RestaurantItem({ restaurant }) {
  return (
    <div className="restaurantItem" id={restaurant.id}>
      <h2 className="AsideHeader">{restaurant.restaurantName}</h2>
      <p className="AsideAddress">{restaurant.address} </p>
    </div>
    <div>
    <p>{restaurant.ratings[0].stars} STAR : </p> {restaurant.ratings[0].comment}
</div>
  );
}

@lich reported correctly, to work with an array, you must use the map method @lich 报告正确,要使用数组,您必须使用 map 方法

using reactjs list look here 使用 reactjs 列表看这里

js arrays js arrays

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM