试图在我的代码的某些部分使用 useState 钩子,但我收到此错误:
Error: Objects are not valid as a React child (found: object with keys {email, text, id}). If you meant to render a collection of children, use an array instead.
删除 useState 括号中的所有内容会使错误消失,但是,为什么这会导致问题??
import React , {createContext, useState} from 'react';
import {v4 as uuid} from 'uuid';
export const CommentContext = createContext();
const CommentContextProvider = (props) => {
const [comments , setComment] = useState([
{email: 's.jahanmard@gmail.com',
text:'Nicest guy eeeeveeer!', id:1},
{email: 'jahanmard22@gmail.com',
text:'buffed as f...', id:2}
]);
const addComment = (email,text) =>{
setComment ([...comments, {email,text,id:uuid()}])
}
return (
<CommentContext.Provider value={{comments , addComment}}>
{props.children}
</CommentContext.Provider>
)
}
export default CommentContextProvider
答案 0 :(得分:0)