React Event Handling
Event handling in React elements is similar to that in DOM elements, but there are some syntactic differences:
- React event binding properties are named in camelCase rather than lowercase.
- With JSX syntax, you need to pass a function as the event handler, not a string (the DOM element approach)
The usual HTML syntax is:
In React, it is written as:
Another difference in React is that you cannot use returning false to prevent the default behavior; you must explicitly use preventDefault.
For example, to prevent a link from opening a new page by default in HTML, you would write:
点我
In React, it is written as:
function
ActionLink
(
)
{
function
handleClick
(
e
)
{
e
.
preventDefault
(
)
;
console
.
log
(
'
Link clicked
'
)
;
}
return
(
<a href="" onClick={handleClick}>Click me
a
)
;
}
In the example, e is a synthetic event.
When using React, you usually do not need to use addEventListener to add a listener to an existing DOM element; you only need to provide a listener when the element is initially rendered.
When you use ES6 class syntax to define a component, the event handler becomes a method of the class. For example, the Toggle component below renders a button that lets the user toggle a switch:
Example
class
Toggle
extends
React
.
Component
{
constructor
(
props
)
{
super
(
props
)
;
this
.
state
=
{
isToggleOn
:
true
}
;
//
The binding here is necessary so that this can be used in the callback function
this
.
handleClick
=
this
.
handleClick
.
bind
(
this
)
;
}
handleClick
(
)
{
this
.
setState
(
prevState
=>
(
{
isToggleOn
: !
prevState
.
isToggleOn
}
)
)
;
}
render
(
)
{
return
(
<button onClick={this>.
handleClick
}
{
this
.
state
.
isToggleOn
?
'
ON
'
:
'
OFF
'
}
button
)
;
}
}
const
root
=
ReactDOM
.
createRoot
(
document
.
getElementById
(
"
root
"
)
)
;
root
.
render
(
<
Toggle
/>,
document.
getElementById
(
'
example
'
)
)
;
Try it »
You must be careful with this in JSX callbacks; class methods do not bind this by default. If you forget to bind this.handleClick and pass it to onClick, the value of this will be undefined when the function is called.
This is not special behavior of React; it is part of how functions work in JavaScript. Generally, if you do not add () after the method, such as onClick={this.handleClick}, you should bind this for that method.
If using bind annoys you, there are two ways to solve this. If you are using the experimental property initializer syntax, you can use a property initializer to correctly bind the callback:
class
LoggingButton
extends
React
.
Component
{
//
This syntax ensures that this is bound in handleClick
//
This is just a test
handleClick
=
(
)
=>
{
console
.
log
(
'
this is:
'
,
this
)
;
}
render
(
)
{
return
(
<button onClick={this>.
handleClick
}
Click
me
button
)
;
}
}
If you are not using the property initializer syntax, you can use an arrow function in the callback:
class
LoggingButton
extends
React
.
Component
{
handleClick
(
)
{
console
.
log
(
'
this is:
'
,
this
)
;
}
render
(
)
{
//
This syntax ensures that this is bound in handleClick
return
(
<button onClick={(e)=>this>.
handleClick
(
e
)
}
Click
me
button
)
;
}
}
The problem with this syntax is that a different callback function is created each time LoggingButton renders. In most cases this is fine. However, if the callback is passed as a prop value to lower-order components, those components may perform extra re-renders. We usually recommend binding in the constructor or using the property initializer syntax to avoid such performance issues.
Passing arguments to event handlers
Usually we pass extra arguments to event handlers. For example, if id is the id of the row you want to delete, both of the following ways can pass arguments to the event handler:
The two ways above are equivalent.
In the two examples above, the parameter e, as the React event object, is passed as the second argument. With the arrow function approach, the event object must be passed explicitly, but with bind, the event object and more arguments are passed implicitly.
It is worth noting that when passing arguments to a listener via bind, for a listener defined in a class component, the event object e must come after the passed arguments, for example:
class
Popper
extends
React
.
Component
{
constructor
(
)
{
super
(
)
;
this
.
state
=
{
name
:
'
Hello world!
'
}
;
}
preventPop
(
name
,
e
)
{
//
The event object e should be placed last
e
.
preventDefault
(
)
;
alert
(
name
)
;
}
render
(
)
{
return
(
<
div
<
p
hello
p
{
/*Pass arguments via the bind() method.
*/
}
<a href="https://reactjs.org" onClick={this>.
preventPop
.
bind
(
this
,
this
.
state
.
name
)
}
Click
a
div
)
;
}
}