[英]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.