×

React Events

React Events

An event is an action that triggers a response of the user action or system-generated event. As HTML, React can also perform actions based on user events. React has the same events as HTML: change, mouse over, click, etc.

React has its event handling system, which is much similar to handling the events on DOM elements. This React event handling system is called as Synthetic events.

React Events

There are some differences between handling the events with react and handling the events with DOM. These are:

  1. The naming convention of the React event is in camelCase instead of lowercase.

For example:

onClick instead of onclick.

  • With JSX, a function is treated as an event handler instead of a string. 

For example:

React event handlers are written in the curly braces:

onClick= {show} instead of onclick=”show()”.

  • Declaration of Event in HTML:
 
  • Declaration of Event in React:
 
  • In react, we cannot return the false for preventing the default behavior, rather than we have to call preventDefault event for preventing the default behavior.

For example:

  • In HTML, for preventing the default link behavior to open a new page, we write as:

Hello
 
  • In React, it can be written as:
function ActionLink() {
function handleClick(e) {
e.preventDefault();
console.log('You visited a Link.');
}
return (

Hello

);
}  

Event Handler Example

The below example defines how to generate an event in React.

import React, { Component } from 'react';
class App extends React.Component {
hello() {
alert("!!!!! Hello World !!!!!");
}
render() {
return (

Event Handler Example

Click the following button to generate the event

); } } export default App;

Output:

Event Handler Example

Once you click on the button, you will get the following output.

Event Handler Example

Child Event

When we require updating the state of the parent component from its child, we can create an event handler (updateState) inside the parent component, and it will pass as a prop (updateStateProp) to the child component where we can call it.

import React from 'react';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
data: 'Hello World!!!!'
} 
this.updateState = this.updateState.bind(this);
};
updateState() {
}
render() {
return (
); } } class Hello extends React.Component { render() { return (

Child Event Example

{this.props.myDataProp}

); } } export default App;

Output:

After compiling the above code, you will get the following output:

Child Event

Once you click on the button, you will get the following output:

Child Event

Related Topics

React State

React State The state is an updatable structure which is used for containing data or information about the component. The state in a component can be changed over time. The change...

3 minutes read.

React JSX

React JSX As we know, all of the React components include a render function. The Render function specifies the HTML output of a React component. JSX is an acronym of JavaScript extension that...

3 minutes read.

React Native vs ReactJS

Difference between React Native and ReactJS React Native React Native is also an open-source JavaScript framework that is used for the development of a mobile application for iOS Android and Windows. It...

6 minutes read.

Difference Between React Flux and MVC

React Flux vs MVC MVC MVC is an acronym of 'Model View Controller.' It is the architectural pattern that is used to develop the user interface. It has three different logical components: The...

2 minutes read.

React Flux

React Flux Introduction It is an application architecture that is internally used by Facebook to build the client-web application with react. It is useful when the project includes dynamic data, and...

2 minutes read.

React Error Boundaries

React Error Boundaries In the past, the errors of JavaScript within the components were used to corrupt the internal state of react and cause it to emit the cryptic errors on the next...

3 minutes read.

React Animation

React Animation The animation is a procedure in which an image is manipulated to appear as a moving image. It is widely used to create an interactive web application. In React, we have to...

4 minutes read.

React Router

What is React Router? Routing is a process by which a user can direct to different pages based on their actions and requests. ReactJS router is generally used to develop single...

10 minutes read.

Difference between Controlled and Uncontrolled component in ReactJs

Controlled Component In the controlled component, the input of the form element is handled by the component rather than the DOM. Controlled components have the functions that govern the data, which...

2 minutes read.

React Bootstrap

React Bootstrap React has a widely used JavaScript framework for creating web applications, and Bootstrap has become the most popular CSS framework. Single-page apps have become popular from the last few...

9 minutes read.

React Refs

React Refs The word ‘Refs’ is an abbreviation of ‘References’ in React. It is as similar as Keys in React. Refs are a function in React which is used for accessing the DOM element and...

7 minutes read.

React CSS

What is React CSS React CSS is used to provide the style to the React applications. The style attribute adds dynamically-computed styles at render time, and it is one of the most used...

5 minutes read.

React Props

React Props A prop is an abbreviation of ‘properties.' State and props are mainly different from each other because props are immutable. Props are the read-only components. It is the object that stores the attribute...

3 minutes read.

React Portals

React Portals React portals were introduced by React 16.0 in September 2017. It gives you a way to render the element outside of the component hierarchy, i.e., within a separate component. Before React 16.0, it...

3 minutes read.

React Conditional Rendering

React Conditional Rendering In React, the working of the conditional rendering is similar to the condition works in JavaScript. We use JavaScript operators for creating elements that represent the current state, and then the React...

3 minutes read.

React Components

React Components Former, the developers used to write more than thousands of lines of code to develop a single-page application. These applications follow the structure of traditional DOM, and it was very challenging to make...

2 minutes read.

React Constructors

What is constructor? The constructor is a method that is used for initializing the state of the object in the class. It calls automatically during the creation of an object in...

4 minutes read.

React Higher-Order Components

React Higher-Order Components In short form, Higher-Order Components are represented as HOC. The Higher-order component is an advanced technique to use component logic. HOC is a function that takes the component and returns the new...

2 minutes read.

Features of ReactJS

ReactJS Features As of now, within the developers, ReactJS is gaining much popularity as the best JavaScript framework. It is crucial for the front-end ecosystem. There are some of the crucial features of ReactJS that are...

2 minutes read.

React Environment Setup Step by Step

React Environment Setup This section will provide you the information about how to set up an environment for the successful development of ReactJS application: Pre-Requisite for ReactJS NodeJS and NPMReact and React DOMWebpackBabel There are two...

5 minutes read.