[英]Objects are not valid as a React child (found: object with keys {arr}). If you meant to render a collection of children, use an array instead
searchf.jsx searchf.jsx
import emojipedia from "./emojipedia";
function Search(props) {
var arr = [];
emojipedia.forEach((element) => {
var flag = 0;
element.keywords.forEach((key) => {
if (key.toLowerCase() === props.toLowerCase()) {
flag = 1;
}
});
if (flag === 1) {
arr.push(element);
}
});
console.log(arr, typeof arr);
return { arr };
}
export default Search;
App.js应用程序.js
var arr = [];
{ arr.map((obj) => {
<button onClick={() => {
console.log(obj.emoji);
setEmo(obj.emoji);
document.getElementsByClassName("cls")[0].style.display =
"block";
}}
className="emoji"
>
{obj.emoji}
</button>;
})}
Search is a function in searchf.jsx to search all the emojis from the data that has the passed argument as keyword. Search 是 searchf.jsx 中的 function,用于从以传递参数为关键字的数据中搜索所有表情符号。 and returns the array of objects.
并返回对象数组。 but I am unable to map through the array error at arr.map in app.js
但我无法通过 app.js 中 arr.map 的数组错误来 map
You are returning an Object
instead of an Array
(Last statement in your search function)您将返回
Object
而不是Array
(搜索功能中的最后一条语句)
Just use只需使用
return arr;
Instead of代替
return { arr };
Your error explains it as well: Objects are not valid as a React child (found: object with keys {arr})您的错误也解释了它:对象作为 React 子级无效(找到:object 和键 {arr})
.map
only works with arrays .map
仅适用于 arrays
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.