简体   繁体   中英

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

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

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. and returns the array of objects. but I am unable to map through the array error at arr.map in app.js

You are returning an Object instead of an Array (Last statement in your search function)

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})

.map only works with arrays

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question 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 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 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 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 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 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 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 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 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM