React add event

React adds events, similar to DOM, but slightly different

For React to add an event, please note:

1. The event of react is named by camel case, and DOM is named by pure lowercase;

2. When using JSX syntax, you need to pass in a function as the event processing function, and DOM passes in a string (although the event name passed in DOM can also be the function name, the data type is still a string)

DOM element add event

<a href="#"Onclick =" testclick(); "> Click me</a>

Add event in React

<button onClick={this.testClick}>Click on me</button>

In React, you can't prevent the default behavior of elements by returning false. You can only prevent the default behavior by setting preventDefault() on the display. There are two ways to prevent the default behavior of elements in DOM: one is that inline scripts directly return false, and the other is that event handlers display calls to the preventDefault method

How to block default behavior in DOM:

<a href=" "onclick =" return false; "> Click me</a>
In this way, you can directly return false to prevent the default behavior of a element from skipping, or you can display and call the preventDefault method in response events. For example:
<!--Note that the parameters passed in need to be fixed event,Cannot change to another value-->
<a href="" onclick="testClick(event);">Click on me</a>
    function testClick(e) {
        console.log("Deal with some other things");

How to prevent the default behavior of elements in React:

    console.log("Deal with other things");
<a href="" onClick={this.testClick}>Click on me</a>

No arguments need to be passed in the event handler

Element binding event in React

There is mainly a problem of this pointing. In the components defined by class inheritance, this in the event processing function does not point to the current component. If we use this directly in the React element, an exception will be reported, because this does not point to the component. Then we need to manually point this to the current component before we can use this, There are four common methods to bind this to the current component:

1. Through arrow function

<a href="" onClick={(e) => this.testClick(e)}>Click on me</a>

2. Use arrow function to write event handling function

testClick = (e) => {
    console.log("Deal with something");
<a href="" onClick={this.testClick}>Click on me.</a>

3. Bind at the call place through bind;

<a href="" onClick={this.handleClickEvent.bind(this)}>Click on me</a>

4. Bind in advance in constructor;

    constructor(props) {
        this.state = {
            date: new Date(),
        this.handleClickEvent = this.handleClickEvent.bind(this);

<a href="" onClick={this.handleClickEvent}>Click on me.</a>

Passing parameters to event handlers

In many operations, we need to pass parameters to the event handler. For example, when deleting or marking a piece of data, we need to pass the ID of the current data to the event handler to let it know which piece of data to process

There are two common ways to transfer values:

1. Way of arrow function

delUser(id, e){
<a href="" onClick={(e) => this.delUser(, e)}>delete</a>

2. Pass parameters to the listener function through bind. Pay attention to the listener function defined in the class component. Event object e should follow the parameters

delUser(id, e){
<a href="" onClick={this.delUser.bind(this,}>delete</a>

In fact, event object e in the event response function is the last in the parameter list, no matter whether you use arrow function or bind to listen to events to pass parameters

goRenren(id,name, e){
<button onClick={(e) => this.goRenren(,, e)}>delete</button>


Tags: Javascript React

Posted on Tue, 14 Apr 2020 03:45:37 -0700 by genista