In this tutorial he uses an onClick function with bind.
<Card onClick={that.deletePerson.bind(null, person)} name={person.name}></Card>
When I remove the bind like this
<Card onClick={that.deletePerson(person)} name={person.name}></Card>
I get an error
Uncaught Error: Invariant Violation: setState(...): Cannot update during an existing state transition (such as within
render
). Render methods should be a pure function of props and state.
I know what bind
does, but why is it needed here? Is the onClick
not calling the function directly?
(code is in this JSbin: https://jsbin.com/gutiwu/1/edit)
He's using the
bind
so that thedeletePerson
method gets the correctperson
argument.Because the
Card
component doesn't get a fullPerson
object, this allows him to identify which person's card was actually clicked.In your example, where you removed the bind
onClick={that.deletePerson(person)}
is actually evaluating the functionthat.deletePerson(person)
and setting that as onClick. ThedeletePerson
method changes the Component's state, which is what the error message is saying. (You can't change state during a render).A better solution might be to pass the id into Card, and pass it back up to the app component on a delete click.