简体   繁体   English

对象作为 React 子问题无效

[英]Objects are not valid as a React child problem

I m having problems when I try to map a json into my react component and I don t know by..当我尝试将 map 和 json 插入我的反应组件时遇到问题,我不知道...

this is the structure:这是结构:

{
  "cards": [
    {
      "id": "1",
      "fares": [
        {
          "fareRef": "1",
          "prices": {
            "afterTax": 100
          }
        }
      ],
      "international": false,
    },
    {
      "id": "2",
      "fares": [
        {
          "fareRef": "1",
          "prices": {
            "afterTax": 200
          }
        }
      ],
      "international": false,
    },

  ]
}

and inside my component I m doing this:在我的组件中,我正在这样做:

 cards.map(element => (
  <p>{element.id}</p> // works!
  <p>{element.fares.map(element => element.prices)}</p> // dosen 't work..
))

Thanks!谢谢!

I am not sure what is the desired behaviour, but right now, you are trying to render an object ( element.prices ) as part of the <p> element.我不确定所需的行为是什么,但现在,您正在尝试将 object ( element.prices ) 作为<p>元素的一部分。 Assuming you are trying to display the afterTax values, you should be referencing the afterTax property within prices .假设您尝试显示afterTax值,您应该在prices中引用afterTax属性。

cards.map(element => (
  <p>{element.id}</p> // works!
  <p>{element.fares.map(element => element.prices.afterTax)}</p>
))

That's because element.fares.prices in your 2nd map call comes out to be:那是因为你的第二个 map 调用中的element.fares.prices是:

"prices": {
            "afterTax": 100
          }

You're rendering an object, which is not allowed.您正在渲染 object,这是不允许的。 Try element.fares.prices.afterTax .试试element.fares.prices.afterTax

cards.map(card => (
  <p>{card.id}</p> // works!
  <p>{card.fares.map(fare => fare.prices.afterTax)}</p> // dosen 't work..
))
cards.map(card => (
  <p>{card.id}</p>
  <p>{card.fares.map(fare => fare.prices)}</p>
))

In general, you should avoid using generic variable names like element to label the items of an iterable to avoid name clashes like this.一般来说,你应该避免使用像element这样的通用变量名称来 label 可迭代的项目以避免这样的名称冲突。

Also, did you mean to render an object?另外,您是要渲染 object 吗? Because fare.prices is an object.因为fare.prices是 object。 Did you mean to render fare.prices.afterTax instead?你的意思是渲染fare.prices.afterTax吗? If that's the case, then it should be:如果是这样的话,那么它应该是:

cards.map(card => (
  <p>{card.id}</p>
  <p>{card.fares.map(fare => fare.prices.afterTax)}</p>
))

You are getting that error because you are creating a new array of objects from element.fares that looks like [ { afterTax: 100 }, { afterTax: 200 }] .您收到该错误是因为您正在从 element.fares 创建一个新的对象数组,看起来像[ { afterTax: 100 }, { afterTax: 200 }] React can't render those objects so it spits out that error. React 无法渲染这些对象,所以它会吐出那个错误。 If you want to see the fares, try something like:如果您想查看票价,请尝试以下操作:

cards.map(element => (
  <p>{element.id}</p>
  <p>{element.fares.map(element => element.prices.afterTax)}</p>
))

Obviously you'll have to format it because this will literally result in 100200 but gives you an idea of what's wrong.显然,您必须对其进行格式化,因为这实际上会导致 100200,但可以让您了解问题所在。

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

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