违反不变性,对象作为 React 子对象无效 [英] Invariant violation, objects are not valid as a React child

查看:37
本文介绍了违反不变性,对象作为 React 子对象无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查了很多类似的问题还没有找到解决方案,我有一个简单的数组,想映射显示

I have checked many similar questions and haven't found a solution yet, I have a simple array and want to map to display

var tt = [
       {
         "age_range": {
           "max": 20,
           "min": 18,
          },
          "city": "San Francisco",
          "first_name": "Carol",
          "gender": "female",
          "id": "sfdf",
          "infinite": "cc",
          "last_name": "Smithberg",
          "locale": "en_US",
          "name": "Carol Albbhgfefjahc Smithberg",
          "option": "Option 3",
          "picture": {
            "data": {
              "height": 960,
              "is_silhouette": true,
              "url": "https://url",
              "width": 1524,
            },
          },
          "sub": "New",
          "uid": "dfee",
          "updated_time": "2017-10-31T23:52:44+0000",
          "verified": false,
       }
    ]

    return tt.map((others, index) =>
      <View key={others.infinite}>
        <Text>{others.sub}</Text>
      </View>
    );

我收到错误:不变量违反,对象作为 React 子对象无效(找到具有键 {_40、_65、_55、_72} 的对象)如果您打算渲染子对象集合,请改用数组.

I'm getting the error: Invariant violation, objects are not valid as a React child (found object with keys {_40, _65, _55, _72}) If you meant to render a collection of children, use an array instead.

{other.sub} 肯定是字符串,我也试过

{other.sub} definitely is a string, and I also tried

return tt.map(({ ...others }, index) =>
  <View key={others.infinite}>
    <Text>{others.sub}</Text>
  </View>
 );

并得到同样的错误,最有希望的解决方案 我发现坚持我作为 {...item} 传递,然后作为 props.{property} 访问,但是我不完全理解这一点,请问我该如何解决?

and got the same error, the most promising solution I've found insists I pass as an {...item} and then access as a props.{property}, however I don't fully understand this, how do I solve this please?

推荐答案

我在使用这个时遇到了同样的错误

I faced the same error, when i used this

render = async () => {

代替这个

render() {

这篇关于违反不变性,对象作为 React 子对象无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆