I'm new in reactjs. I'm trying to add a onClick event in my child component like:
## BlogList.js
<button onClick={handleDelete(blog.id)}>delete</button>
the handleDelete function in the parent component is:
## Home.js
const handleDelete = (id) => {
const newBlogs = blogs.filter((blog) => blog.id !== id)
setBlogs(newBlogs)
}
The error message is:
Cannot update a component (Home) while rendering a different component (BlogList). To locate the bad setState() call inside BlogList
Why didn't it work? Why did it have to write like <button onClick={()=>handleDelete(blog.id)}>delete</button> ?
onClick={handleDelete}in-case you don't want to pass any params. But if you want to pass in params inside the function you have to use that syntaxonClick={()=>handleDelete(blog.id)}. On the first approach I mentioned you are simply passing in all the events associated with that button