onClick
receives value as its function
to trigger/call/run when user "click" on it. But in your code, you trigger/call/run function this.delete()
during compile phase (compiler read Button onClick={this.delete(id)}>Delete Button</button>
and called this.delete(id)
immediately and use the result of it as onClick's value(delete function return nothing it means {return undefined}
, so the final result is ` ).
So the result is:
- Everytime the component loaded, it will call delete instantly. And
onClick
value now is undefined
and it makes onClick
won't work as expected.
For more details I'll give an example below:
const DeleteButton =({id})=> {
const delete = (id)=> {
return api.deleteItem(id)
}
return <Button onClick={delete(id)}>Delete Button</button>
}
So, when I use the component above:
<Container>
<Content value={item}/>
<DeleteButton id={item.id}/>
</Container>
it will automatically delete the item you've loaded, because while render DeleteButton
it called delete(id)
already.
So, how to fix it? - there are many solutions for it, but ultimately it have to give the type of value of onClick
is a function
:
#1 I bet noone use this, but I think it is more useful to describe my idea.
const DeleteButton =({id})=> {
const delete = (id)=> {
return function() {
api.deleteItem(id)
}
}
//delete(id) called and it returns an anonymous function that receive `id` and called it when onClick trigger
return <Button onClick={delete(id)}>Delete Button</button>
}
#2
const DeleteButton =({id})=> {
const delete = (id)=> {
return api.deleteItem(id)
}
return <Button onClick={(id)=>delete(item.id)}>Delete Button</button>
//or use bind : bind is return a function that wrapped parameter into
return <Button onClick={delete.bind(id)}>Delete Button</button>
}