Answer the question
In order to leave comments, you need to log in
How does props forwarding work here?
I'm learning React from video tutorials. Apparently, the author forgot to tell something.
The code below is working, but I do not have sufficient understanding of some points.
Profile.jsx
const Profile = () => {
/* Информация для сообщений - текст сообщений и лайки */
let posts = [
{id: 1, message: "Пост 1", likesCount:1},
{id: 2, message: "Пост 2", likesCount:2},
]
return (
<div>
<MyPosts posts={posts} />
</div>
);
}
const MyPosts = (props) =>
{
let postsElements = props.posts.map( p => <Post message={p.message} likesCount={p.likesCount} /> )
return (
<div className={css.MyPosts}>
<div className={css.posts}>
/* Вывод сообщений */
{postsElements}
</div>
</div>
);
}
Answer the question
In order to leave comments, you need to log in
postsElements
becomes an array of components <Post>
. In this case, .map
turns each set of data from the array props.posts
into a component with this data. The array itself props.post
does not change in any way. Read about .map
Modifying props is not a good idea
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question