简体   繁体   English

无法显示 ReactJS 中的数据。错误:对象作为 React 子项无效。 如果您打算呈现子集合,请改用数组

[英]Cannot display data in ReactJS. Error: Objects are not valid as a React child. If you meant to render a collection of children, use an array instead

I'm trying to displaying data from my backend, but I'm getting error: Error: Objects are not valid as a React child (found: object with keys {id, price, descr, link, sqf}).我正在尝试显示来自后端的数据,但出现错误:错误:对象作为 React 子项无效(找到:object,键为 {id、price、descr、link、sqf})。 If you meant to render a collection of children, use an array instead.如果您打算渲染子集合,请改用数组。

I've been searching for solution and I came across.map but it doesn't work throws again an error.我一直在寻找解决方案,我遇到了。map 但它不起作用再次引发错误。

import React, { useState, useEffect } from 'react';
import ArticleService from '../services/articles';

const Dashboard = () => {
  const [content, setContent] = useState('');

  useEffect(() => {
    ArticleService.articles().then(
      (response) => {
        setContent(response.data);
      },
      (error) => {
        const _content =
          (error.response &&
            error.response.data &&
            error.response.data.message) ||
          error.message ||
          error.toString();

        setContent(_content);
      }
    );
  }, []);

  console.log(content);
  return (
    <div className='container'>
      <h3>{content}</h3>
    </div>
  );
};

export default Dashboard;

console.log of content looks like this:内容的 console.log 如下所示: 控制台日志

As you can see, I get content regularly I just can't display it on dashboard page.如您所见,我定期获取内容,只是无法在仪表板页面上显示。 When I try {content.price} it removes an error but still doesn't show nothing.当我尝试{content.price}时,它消除了一个错误,但仍然没有显示任何内容。 Any solutions???有什么解决办法吗???

According to your log, content is an array of objects so you want to map the array to valid JSX, for example if you want to render in a div element:根据您的日志, content是一个对象数组,因此您希望将数组 map 转换为有效的 JSX,例如,如果您想在div元素中呈现:

<div className="container">
  <h3>
    {content?.map((item) => (
      <div key={item.id}>
        <div>{item.price}</div>
        <div>{item.descr}</div>
      </div>
    ))}
  </h3>
</div>

In addition, the initial state should be an empty array: useState([]) to match the expected data:此外,初始的 state 应该是一个空数组: useState([])来匹配预期的数据:

const Dashboard = () => {
  const [content, setContent] = useState([]);
  ...
};

暂无
暂无

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

相关问题 ReactJS 错误:对象作为 React 子项无效。 如果您打算渲染一组孩子,请改用数组 - ReactJS Error: Objects are not valid as a React child. If you meant to render a collection of children, use an array instead 对象作为 React 子级无效。 如果您打算渲染一组子项,请改用数组 - 错误 Solidity - React - Objects are not valid as a React child. If you meant to render a collection of children, use an array instead - Error Solidity - React 错误:对象作为 React 子项无效。 如果您打算渲染一组孩子,请改用数组 - Error: Objects are not valid as a React child. If you meant to render a collection of children, use an array instead “对象作为 React 子对象是无效的。 如果您打算渲染一组子项,请改用数组。” 错误 - “Objects are not valid as a React child. If you meant to render a collection of children, use an array instead.” error 如何修复错误:对象作为 React 子对象无效。 如果您打算渲染一组孩子,请改用数组 - How to fix error: Objects are not valid as a React child. If you meant to render a collection of children, use an array instead 错误:对象作为 React 子对象无效。 如果您打算渲染一组子项,请改用数组。 从 JSON 获取数据 - Error: Objects are not valid as a React child. If you meant to render a collection of children, use an array instead. Getting data from JSON 对象作为 React 子项无效。 如果您打算呈现子集合,请改用数组 - Objects are not valid as a React child. If you meant to render a collection of children, use an array instead 对象作为 React 子项无效。 如果您打算渲染子集合,请改用数组。 如何退货? - Objects are not valid as a React child. If you meant to render a collection of children, use an array instead. How to return it? 未捕获的不变违规:对象无效作为React子代。 如果要渲染子级集合,请改用数组 - Uncaught Invariant Violation: Objects are not valid as a React child. If you meant to render a collection of children, use an array instead 对象作为 React 子对象无效。 如果您打算渲染一组子项,请改用数组 - FlatList - Objects are not valid as a React child. If you meant to render a collection of children, use an array instead - FlatList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM