我正在设置一个带有Rails后端的React应用程序。我得到的错误“对象是无效的React子(发现:对象与键{id,名称,信息,created_at, updated_at})。如果你想呈现一组子元素,请使用数组。”

这是我的数据:

[
    {
        "id": 1,
        "name": "Home Page",
        "info": "This little bit of info is being loaded from a Rails 
        API.",
        "created_at": "2018-09-18T16:39:22.184Z",
        "updated_at": "2018-09-18T16:39:22.184Z"
    }
]

我的代码如下:

import React from 'react';

class Home extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      error: null,
      isLoaded: false,
      homes: []
    };
  }

  componentDidMount() {
    fetch('http://localhost:3000/api/homes')
      .then(res => res.json())
      .then(
        (result) => {
          this.setState({
            isLoaded: true,
            homes: result
          });
        },
        // error handler
        (error) => {
          this.setState({
            isLoaded: true,
            error
          });
        }
      )
  }

  render() {

    const { error, isLoaded, homes } = this.state;

    if (error) {
      return (
        <div className="col">
          Error: {error.message}
        </div>
      );
    } else if (!isLoaded) {
      return (
        <div className="col">
          Loading...
        </div>
      );
    } else {
      return (
        <div className="col">
          <h1>Mi Casa</h1>
          <p>This is my house y'all!</p>
          <p>Stuff: {homes}</p>
        </div>
      );
    }
  }
}

export default Home;

我做错了什么?


当前回答

我也遇到过同样的问题,但现在我很高兴解决了这个问题。

NPM I core-js 把这一行放到index.js文件的第一行。 进口core-js

其他回答

只是添加到其他选项,我试图通过点方法访问主对象内的嵌套对象,如: this.state.arrayData.CompleteAdress.Location 在这种情况下,位置是完整地址内嵌套的对象,这就是为什么我不能简单地用点符号访问它。

因此,如果您面临同样的问题,请尝试JSON。解析,以便访问嵌套对象,然后进行相应操作。

好吧,在我的情况下,我想渲染的数据包含一个对象在数组中,因此,它会给出错误,所以对于其他人来说,请检查你的数据也一次,如果它包含一个对象,你需要将它转换为数组打印它的所有值,或者如果你需要一个特定的值,然后使用。

我的数据:

body: " d fvsdv"
photo: "http://res.cloudinary.com/imvr7/image/upload/v1591563988/hhanfhiyalwnv231oweg.png"
postedby: {_id: "5edbf948cdfafc4e38e74081", name: "vit"}       
//this is the object I am talking about.
title: "c sx "
__v: 0
_id: "5edd56d7e64a9e58acfd499f"
__proto__: Object

只打印单个值

<h5>{item.postedby.name}</h5>

就我而言,我做到了

<IonListHeader>
  <IonLabel>
     Order {{index}}
  </IonLabel>
</IonListHeader>

而不是

<IonListHeader>
  <IonLabel>
     Order {index}
  </IonLabel>
</IonListHeader>

双花括号。

在您的状态下,home被初始化为一个数组 家庭:[]

在返回中,尝试呈现home(这是一个数组)。 < p >东西:{家园}< / p >

不能这样做——如果你想渲染它,你需要渲染一个数组到每个单独的项目。例如:使用map()

Ex:{家(item = >)的项目文件夹。

我有一个类似的错误,而我正在创建一个自定义模式。

const CustomModal = (visible, modalText, modalHeader) => {}

问题是我忘记了功能组件只能有一个道具传递给他们,根据REACT文档:

这个函数是一个有效的React组件,因为它只接受一个 “props”(代表属性)对象参数与数据和 返回一个React元素。我们称这种组件为“功能组件” 因为它们是JavaScript函数。反应文档

Therefore when we want to pass several variables we need to wrap them into an object or an Array and pass the pointer to that array or object. And destructure it on the component side before invoking the component. Alternatively we can use curly braces to indicate that we are sending an object with identical property names and variables that contain the value of those properties, like in the example here. And then define the function also to destructure upon arrival of the properties contained in the object.

const CustomModal = ({visible, modalText, modalHeader}) => {}

如果你有多个值要传递给组件,你应该传递一个对象,在它的属性/变量周围用花括号括起来(假设它们有相同的名字)。