admin管理员组文章数量:1399754
im tring to fetch data from a file called dummyData.js It has the following structure.
export const Posts = [
{
id: 1,
desc: "Love For All, Hatred For None.",
photo: "assets/post/1.jpeg",
date: "5 mins ago",
userId: 1,
like: 32,
ment: 9,
},
{
id: 2,
photo: "assets/post/2.jpeg",
date: "15 mins ago",
userId: 2,
like: 2,
ment: 1,
},
Now i am mapping the data from that file.
export default function Feed() {
return (
<div className="feed">
<div className="feedWrapper">
<Share/>
{Posts.map((p) => (
<Post key={p.id} post={p}/>
))}
</div>
</div>
)
}
here is my post ponent
export default function Post({post}) {
console.log(post);
return (
<div>
<div className="post">
<div className="postWrapper">
<div className="postTop">
<div className="topLeft">
<img className="profilePic" src="/files/prof.png" alt="" />
<span className="postUsername">Kasun Gayantha</span>
<span className="postDate"> 5 mins ago</span>
</div>
<div className="topRight">
<MoreVert/>
</div>
</div>
<div className="postCenter">
<span className="postText">hey this is first post</span>
<img src="/files/prof.png" alt="" className="postImage" />
</div>
<div className="postBottom">
<div className="postBottomLeft">
<ThumbUpAltIcon htmlColor="blue" className="likeIcon"/>
<FavoriteIcon htmlColor="red" className="likeIcon"/>
<span className="postLikeCounter">32 people like it </span>
</div>
<div className="postBottomRight">
<span className="postCommentText"> ments</span>
</div>
</div>
</div>
</div>
</div>
)
}
I have ponents called feed.jsx,Post.jsx
but when I pile this the following error es up
TypeError: Cannot read properties of undefined (reading 'map')
return (
9 | <div className="feed">
10 | <div className="feedWrapper">
> 11 | <Share/>
^ 12 | {Posts.map((p) => (
13 | <Post key={p.id} post={p}/>
14 | ))}
error is in from the line no 11. Im new to react , struggling to fix this. I have looked at many solutions on the internet.
im tring to fetch data from a file called dummyData.js It has the following structure.
export const Posts = [
{
id: 1,
desc: "Love For All, Hatred For None.",
photo: "assets/post/1.jpeg",
date: "5 mins ago",
userId: 1,
like: 32,
ment: 9,
},
{
id: 2,
photo: "assets/post/2.jpeg",
date: "15 mins ago",
userId: 2,
like: 2,
ment: 1,
},
Now i am mapping the data from that file.
export default function Feed() {
return (
<div className="feed">
<div className="feedWrapper">
<Share/>
{Posts.map((p) => (
<Post key={p.id} post={p}/>
))}
</div>
</div>
)
}
here is my post ponent
export default function Post({post}) {
console.log(post);
return (
<div>
<div className="post">
<div className="postWrapper">
<div className="postTop">
<div className="topLeft">
<img className="profilePic" src="/files/prof.png" alt="" />
<span className="postUsername">Kasun Gayantha</span>
<span className="postDate"> 5 mins ago</span>
</div>
<div className="topRight">
<MoreVert/>
</div>
</div>
<div className="postCenter">
<span className="postText">hey this is first post</span>
<img src="/files/prof.png" alt="" className="postImage" />
</div>
<div className="postBottom">
<div className="postBottomLeft">
<ThumbUpAltIcon htmlColor="blue" className="likeIcon"/>
<FavoriteIcon htmlColor="red" className="likeIcon"/>
<span className="postLikeCounter">32 people like it </span>
</div>
<div className="postBottomRight">
<span className="postCommentText"> ments</span>
</div>
</div>
</div>
</div>
</div>
)
}
I have ponents called feed.jsx,Post.jsx
but when I pile this the following error es up
TypeError: Cannot read properties of undefined (reading 'map')
return (
9 | <div className="feed">
10 | <div className="feedWrapper">
> 11 | <Share/>
^ 12 | {Posts.map((p) => (
13 | <Post key={p.id} post={p}/>
14 | ))}
error is in from the line no 11. Im new to react , struggling to fix this. I have looked at many solutions on the internet.
Share Improve this question asked Sep 6, 2021 at 3:19 Banchi SecretBanchi Secret 211 gold badge1 silver badge3 bronze badges 1-
How to import
Posts
? – Viet Commented Sep 6, 2021 at 3:42
3 Answers
Reset to default 3You are getting this error because the Posts
array is undefined
.
Do (Posts || []).map(.....)
so that the .map
is not called on an undefined
variable.
You can do something like:
return (
<div className="feed">
<div className="feedWrapper">
<Share />
{(Posts || []).map(p => (
<Post key={p.id} post={p} />
))}
</div>
</div>
);
This error shows up when you’re trying to use .map
on an array, but that array isn’t defined yet.
You could try these options
- Defining
Posts
as an empty array inside the state. - You could condionally render
Post
ponent based on its lengthreturn ( <div className="feed"> <div className="feedWrapper"> <Share/> {Posts.length && Posts.map((p) => ( <Post key={p.id} post={p}/> )) } </div> </div> )
Always check for the length before looping through
export default function Feed() {
return (
<div className="feed">
<div className="feedWrapper">
<Share/>
{ Posts && Posts.length ?
Posts.map((p) => (
<Post key={p.id} post={p}/>
)) : null}
</div>
</div>
)
}
本文标签:
版权声明:本文标题:javascript - TypeError: Cannot read properties of undefined (reading 'map') ReactJs Getting data from a file - S 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744136548a2592423.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论