简体   繁体   English

对象作为 React 子级无效(找到:object 和键 {arr})。 如果您打算渲染一组孩子,请改用数组

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

相关问题 对象作为 React 子级无效(找到:object 和键 {children})。 如果您打算渲染一组孩子,请改用数组 - Objects are not valid as a React child (found: object with keys {children}). If you meant to render a collection of children, use an array instead 对象作为 React 子级无效(找到:object 和键 {value})。 如果您打算渲染一组孩子,请改用数组 - Objects are not valid as a React child (found: object with keys {value}). If you meant to render a collection of children, use an array instead 对象作为 React 子级无效(找到:object 和键 {weight})。 如果您打算渲染一组孩子,请改用数组 - Objects are not valid as a React child (found: object with keys {weight}). If you meant to render a collection of children, use an array instead 对象作为 React 子级无效(找到:object 和键 {_delegate})。 如果您打算渲染一组孩子,请改用数组 - Objects are not valid as a React child (found: object with keys {_delegate}). If you meant to render a collection of children, use an array instead 对象作为React子对象无效(找到:带有键{this}的对象)。 如果要渲染子级集合,请改用数组 - Objects are not valid as a React child (found: object with keys {this}). If you meant to render a collection of children, use an array instead 对象作为 React 子项无效(找到:object 和键 {totalItems})。 如果您打算渲染一组孩子,请改用数组 - Objects are not valid as a React child (found: object with keys {totalItems}). If you meant to render a collection of children, use an array instead 如何修复对象作为 React 子对象无效(找到:带有键 {} 的对象)。 如果您打算渲染一组子项,请改用数组 - How to fix Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead 错误:对象作为 React 子级无效(找到:object 和键 {rank})。 如果您打算渲染一组孩子,请改用数组 - Error: Objects are not valid as a React child (found: object with keys {rank}). If you meant to render a collection of children, use an array instead 错误:对象作为 React 子项无效(找到:object,键为 {})。 如果您打算渲染子集合,请改用数组。 JS - Error: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead. JS Uncaught Error:Objects are not valid as a React child (found: object with keys.If you meant to render a collection of children, use an array instead 未捕获的错误:对象作为 React 子项无效(已找到:带键的对象。如果您打算呈现子项集合,请改用数组 - Uncaught Error:Objects are not valid as a React child (found: object with keys.If you meant to render a collection of children, use an array instead
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM