I'm learning React and I recently encountered a bug that I couldn't trace.
Warning: Cannot update a component (`App`) while rendering a different component (`UserDisplayItem`). To locate the bad setState() call inside `UserDisplayItem
I then discovered that I was directly calling a passed down prop function within a div.
import React from "react";
import Card from "../Card/Card";
const UserDisplayItem = (props) => {
return (
<Card>
<div onClick={ props.onDeleteUser(props.id)}>
<div>{props.user.userName}</div>
<div>{props.user.age}</div>
</div>
</Card>
);
};
export default UserDisplayItem;
It then worked once I added a function to handle it:
import React from "react";
import Card from "../Card/Card";
const UserDisplayItem = (props) => {
// !!!!!!!! VERY IMPORTANT. ALWAYS MAKE YOUR OWN FUNCTION
const deleteHandler = () => {
props.onDeleteUser(props.id);
};
return (
<Card>
<div onClick={deleteHandler}>
<div>{props.user.userName}</div>
<div>{props.user.age}</div>
</div>
</Card>
);
};
export default UserDisplayItem;
So I'm happy it works but I want to understand why the other method didn't work. Any help will be appreciated, thanks.