简体   繁体   English

错误:对象作为 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

guys!伙计们! I'm using ReactJS to create a small website.我正在使用 ReactJS 创建一个小型网站。 Since I added the following code it starts showing an error: Objects are not valid as a React child.由于我添加了以下代码,它开始显示错误: Objects are not valid as a React child。 If you meant to render a collection of children, use an array instead.如果您打算渲染一组子项,请改用数组。

Code:代码:

import { useState, useEffect } from 'react';
import { motion, useAnimation } from 'framer-motion';
import './css/App.min.css';
import config from './config';

function App() {
  return (
    <div className="myskills">
      <Skills skillType="coding" />
    </div>
  );
}

function Skills(props){
  const skillType = props.skillType;
  const result = config.skills.filter(skill => skill.cat == skillType);
  console.log(result);
  result.map((skill, index) => (
    <div className="singleSkill" key={index}>
      {skill.name} Level: {skill.level}
    </div>
  ));
  return (<div>{result}</div>);
}

config.json配置文件

{
  "skills": [
    {
      "name": "HTML",
      "level": 5,
      "cat": "coding"
    },
    {
      "name": "CSS",
      "level": 5,
      "cat": "coding"
    },
    {
      "name": "PHP",
      "level": 4,
      "cat": "coding"
    }
  ]
}

Any ideas what's the problem?任何想法有什么问题?

The return statement in your Skills component is basically just this: Skills组件中的 return 语句基本上就是这样:

return (config.skills.filter(skill => skill.cat == skillType));

hence the "Objects are not valid as a React child" error.因此“对象作为 React 子对象无效”错误。

Since result.map doesn't modify the original array, a better solution might look something like this:由于result.map不会修改原始数组,因此更好的解决方案可能如下所示:

function Skills(props) {
  const skillType = props.skillType;
  const result = config.skills.filter(skill => skill.cat == skillType);
  return (
    <div>
      {result.map((skill, index) => (
        <div className="singleSkill" key={index}>
          {skill.name} Level: {skill.level}
        </div>
      ))}
    </div>
  );
}

暂无
暂无

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

相关问题 “对象作为 React 子对象是无效的。 如果您打算渲染一组子项,请改用数组。” 错误 - “Objects are not valid as a React child. If you meant to render a collection of children, use an array instead.” error 对象作为 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 子级无效。 如果您打算渲染一组子项,请改用数组 - 错误 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 子对象无效。 如果您打算渲染一组孩子,请改用数组 - 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 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 无法显示 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 对象作为 React 子项无效。 如果您打算呈现子集合,请改用数组 - Objects are not valid as a React child. If you meant to render a collection of children, use an array instead 未捕获的不变违规:对象无效作为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