Next.js Discord

Discord Forum

map not working on list

Answered
Black-whiskered Vireo posted this in #help-forum
Open in Discord
Avatar
Black-whiskered VireoOP
what am I doing wrong here?
Image
Answered by B33fb0n3
yes and one thing that I forgot:
export default async function Home() {
  const posts = await getAllPosts();
  return (
    <>
{
      posts.map((post) =>
      {
        return <Post
          id={post.id}
          subject={post.subject}
          comment={post.comment}
          filePath={post.filePath}
        />
      }
      )
}
    </>
  );
}
View full answer

27 Replies

Avatar
Black-whiskered VireoOP
I am getting data from prisma in the get all posts function, since its find many, its a list.. now I want to make multiple post components based on the post list
Avatar
do it like this:
#.map((post) => {
  return <Post {..your props..} />
}
Avatar
Black-whiskered VireoOP
#.map?
Avatar
posts.map....
Avatar
Black-whiskered VireoOP
okk
export default async function Home() {
  const posts = await getAllPosts();
  return (
    <>
      posts.map((post) =>
      {
        return <Post
          id={post.id}
          subject={post.subject}
          comment={post.comment}
          filePath={post.filePath}
        />
      }
      )
    </>
  );
}
sorry you have to spoon feed
but is it like this?
@B33fb0n3
Avatar
yes and one thing that I forgot:
export default async function Home() {
  const posts = await getAllPosts();
  return (
    <>
{
      posts.map((post) =>
      {
        return <Post
          id={post.id}
          subject={post.subject}
          comment={post.comment}
          filePath={post.filePath}
        />
      }
      )
}
    </>
  );
}
Answer
Avatar
Black-whiskered VireoOP
missing key prop for element in iterator
there is a quick fix to disable react jsx key
Avatar
give it a key then
with post.id
Avatar
Black-whiskered VireoOP
this is my post component btw
Image
key = post.id?
Image
I can post code too
Avatar
your Post component needs to return something so you be able to use it as componnet
Avatar
yes but you also keep your id=post.id
and i think where you define your Post component it might be linter messing up
where you have return;
#Unknown Channel
</>
do instead
return ( #Unknown Channel </> )
Avatar
Black-whiskered VireoOP
thank you all very much
I have a different question now, so ill ask it in a different post
Avatar
happy to help