React Event
React Event Handling
Event Handling
- Use camel case for event handling attribute. e.g. onClick instead of onclick
- Pass a function, don’t pass a function call
Example
1 | function ClickButton() { |
You can also use in-line functions as event handler
1 | function SayHello() { |
Passing Arguments to Event Handler
Here is an example to pass id
to the function. the handle function will take parameter id
and event e as parameter
1 | <button onClick={(e) => handle(id, e)}>Delete</button> |
if event e is not needed:
1 | <button onClick={() => handle(id)}>Delete</button> |
Prevent Default
To prevent the default behavior, you must call preventDefault
explicitly.
1 | function ActionLink() { |