11

the provided answers didn't give me a hint on whats the problem in my case here. So im getting the following error in console:

react-jsx-dev-runtime.development.js?bfcc:117 Warning: Each child in a list should have a unique "key" prop.

Check the render method of `Feed`. See https://reactjs.org/link/warning-keys for more information.

This is my code in the return statement:

return (
    <>
      <Header></Header>
      <div className={hfstyles.feed} style={{ 'maxWidth': '980px;', 'textAlign': 'center', 'margin': '0 auto', 'padding': '5em 0' }}>

        {feed.map((post, index) => {
          const postBefore = index == 0 ? new Date() : feed[index - 1].createdAt;
          return randomIndex == index ? (
            <>
            <PostBasic
              post={post}
              postBefore={postBefore}
              key={post.id}
            />
            <RandomMessage/>
            </>)
            :
            (<PostBasic
              post={post}
              postBefore={postBefore}
              key={post.id}
            />
          );
        })}

        {feedEnd == true && (
          <p>Thats it</p>
        )}

      </div>
      <Footer sticky={false}></Footer>
    </>
  );

I also tried to provide a key property to the <RandomMessage/> component and I tried to change the key from database id post.id to the index of the map without any change

Marcel Dz
  • 2,321
  • 4
  • 14
  • 49

1 Answers1

33

you have to pass key props to fragment(<>), not to the PostBasic.

Use like this,

Change this

(
  <>
    <PostBasic post={post} postBefore={postBefore} key={post.id} />
    <RandomMessage />
  </>
);

TO

(<React.Fragment key={post.id}>
  <PostBasic post={post} postBefore={postBefore} />
  <RandomMessage />
</React.Fragment>
)
Rahul Sharma
  • 9,534
  • 1
  • 15
  • 37