Home > Back-end >  TypeError: .map is not a function don't understand what's wrong
TypeError: .map is not a function don't understand what's wrong

Time:10-31

I don't understand why this component doesn't work Here is link to img "because somehow i can't post image directly [1]: https://i.stack.imgur.com/Ax7im.png Thank's for any help Here you have code:

import React from 'react';
import ReactDOM from 'react-dom';
import Postitem from "./Postitem";

const Postlist=(post,title)=>{

return (
<div>
         <h1 style={{textAlign: 'center'}}>{title}</h1>
{post.map((post) =>
 <Postitem post={post} key={post.id}/>)};
</div>
 
)};
export default Postlist;
      

CodePudding user response:

React function components only ever take one argument, which is an object containing all the props. Assuming you are passing a post prop as an array when you are rendering it, fixing your function declaration to this will work:

const Postlist=({post,title})=>{
  // component body here, as above
}

CodePudding user response:

Check, what is coming in the "post" variable to this component. If you use "post.map" function, then it must be an array.

  • Related