Dung (Donny) Nguyen

Senior Software Engineer

Events Handling

Handling events in React.js is very similar to handling events in plain JavaScript, but with some key differences and improvements. In React, we can define event handlers as methods on our component and bind them to elements using JSX syntax. Here are the steps and examples to help us understand how to handle events in React.js:

Steps to Handle Events

  1. Define an Event Handler: Create a function to handle the event. This function can be defined either inside our component class (for class components) or directly within our function component.

  2. Bind the Event Handler: Use JSX to bind our event handler to a specific event on a DOM element.

  3. Use Synthetic Events: React uses a synthetic event system that wraps the native event system. This ensures that events work consistently across different browsers.

Examples

Functional Component with Event Handler

Here’s an example using a functional component with the onClick event:

import React from 'react';

function ButtonClick() {
  // Define the event handler function
  const handleClick = () => {
    alert('Button was clicked!');
  };

  // Bind the event handler to the button's onClick event
  return <button onClick={handleClick}>Click Me</button>;
}

export default ButtonClick;

In this example:

Class Component with Event Handler

Here’s an example using a class component:

import React, { Component } from 'react';

class ButtonClick extends Component {
  // Define the event handler as a class method
  handleClick() {
    alert('Button was clicked!');
  }

  render() {
    // Bind the event handler to the button's onClick event using `this`
    return <button onClick={this.handleClick.bind(this)}>Click Me</button>;
  }
}

export default ButtonClick;

In this example:

Using Arrow Functions to Avoid Binding

We can use arrow functions to avoid explicit binding in class components:

import React, { Component } from 'react';

class ButtonClick extends Component {
  // Define the event handler using an arrow function
  handleClick = () => {
    alert('Button was clicked!');
  };

  render() {
    // Bind the event handler to the button's onClick event
    return <button onClick={this.handleClick}>Click Me</button>;
  }
}

export default ButtonClick;

In this example:

Common Events in React

Example with Form Submission

Here’s an example of handling form submission in React:

import React, { useState } from 'react';

function FormSubmit() {
  const [value, setValue] = useState('');

  const handleChange = (event) => {
    setValue(event.target.value);
  };

  const handleSubmit = (event) => {
    alert('A name was submitted: ' + value);
    event.preventDefault();
  };

  return (
    <form onSubmit={handleSubmit}>
      <label>
        Name:
        <input type="text" value={value} onChange={handleChange} />
      </label>
      <button type="submit">Submit</button>
    </form>
  );
}

export default FormSubmit;

In this example:

By using these patterns, we can handle various events in React.js efficiently and create interactive user interfaces.