I'm new to React. It might be more JS question than React question tho. When I write the code in three different ways for this part, they all work fine showing "ID is 1" in the console.
1
<Post click={() => this.postSelectedHandler(post.id)}/>
2
<Post click={() => {this.postSelectedHandler(post.id)}}/>
3
<Post click={() => {return this.postSelectedHandler(post.id)}}/>
My question is why they all work fine. Especially #3 returns the function and I don't understand why it works in the same way as the other two. About #1 & #2, with or without {} doesn't matter?
original code (same as #1)
postSelectedHandler = id => {
console.log("ID is " + id);
};
render() {
const posts = this.state.posts.map(post => {
return (
<Post click={() => this.postSelectedHandler(post.id)}/>
);
});
return (
<div>
<section>{posts}</section>
</div>
);
}
Thank you for your help.