简体   繁体   中英

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-dom.development.js:14887 Uncaught Error: 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.

This is the object

const post = [
{
  id: 1,
name: "Farish Jamal",
description: "Web Developer",
time: new Date().toLocaleTimeString(),
message: "This is forat",
photoUrl: "",
timestamp: firebase.firestore.FieldValue.serverTimestamp()
},
{
  id: 6,
name: "Farish Jamal",
description: "Web Developer",
time: new Date().toLocaleTimeString(),
message: "This is forat",
photoUrl: "",
timestamp: firebase.firestore.FieldValue.serverTimestamp()
},]

This is where i am fetching the data

<div className='feed__post'>
    {post.map(({id, name, description, message, photoUrl, timestamp }) =>(
        <Post 
        key={id}
        name={name}
        description={description}
        message={message}
        photoUrl={photoUrl}
        time={timestamp}
        />
    ))}
    </div>

Your timestamp value firebase.firestore.FieldValue.serverTimestamp() is returning (what is being considered) an object and the time prop is not being processed properly by your <Post> component.

Does the error go away when you remove this time={timestamp}

<div className='feed__post'>
    {post.map(({id, name, description, message, photoUrl, timestamp }) =>(
        <Post 
        key={id}
        name={name}
        description={description}
        message={message}
        photoUrl={photoUrl}
        />
    ))}
    </div>

I'm not 100% on exactly the best way to process the object returned from serverTimestamp() but I'm referring to this post suggesting:

firebase.firestore.FieldValue.serverTimestamp().toDate() which will give you a JS Date object, which you should be able to process into a string with .toLocaleTimeString()

How to get a JavaScript Date object from the new `firebase.firestore.FieldValue.serverTimestamp()`

firebase.firestore.FieldValue.serverTimestamp().toDate().toLocaleTimeString()

all elements is inside in one object and you need to check the length first...

<div className='feed__post'>
    {(post.length > 0) && post.map(item =>(
        <Post 
          key={item.id}
          name={item.name}
          description={item.description}
          message={item.message}
          photoUrl={item.photoUrl}
          time={item.timestamp}/>
    ))}
</div>

you should pass any of objects in your object array in map function:

<div className='feed__post'>
    {post.map(item =>(
        <Post 
        key={item.id}
        name={item.name}
        description={item.description}
        message={item.message}
        photoUrl={item.photoUrl}
        time={item.timestamp}/>
    ))}
</div>

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 {arr}). 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