Skip to main content

What is event in react js?

 In React.js, an event is an object that represents an action or occurrence, such as a user interaction (e.g., a mouse click, a key press, or a form submission). React handles events in a declarative and efficient way, providing a synthetic event system that is consistent across different browsers.

Key Features of Events in React:

  1. Synthetic Events: React wraps native browser events in its own event system called SyntheticEvent. This ensures compatibility across browsers and provides additional features like event pooling for performance optimization.

  2. Event Handling:

    • Events in React are written as camelCase (e.g., onClick, onChange), unlike plain HTML where events are written in lowercase (e.g., onclick, onchange).
    • Event handlers are passed as functions, typically as references or inline arrow functions.
  3. Binding this:

    • When using class components, you may need to bind the event handler to the component instance (this).
    • In function components, this is not required, especially when using hooks like useState or useReducer.
  4. Prevent Default Behavior:

    • Use event.preventDefault() to prevent the default action of an event (e.g., stopping a form from submitting).
  5. Stop Propagation:

    • Use event.stopPropagation() to stop the event from bubbling up to parent elements.

Example of Event Handling in React:

Functional Component Example:

jsx

import React, { useState } from 'react'; function ClickCounter() { const [count, setCount] = useState(0); const handleClick = () => { setCount(count + 1); }; return ( <div> <button onClick={handleClick}>Click me</button> <p>You clicked {count} times</p> </div> ); } export default ClickCounter;

Class Component Example:

jsx

import React, { Component } from 'react'; class ClickCounter extends Component { constructor(props) { super(props); this.state = { count: 0 }; this.handleClick = this.handleClick.bind(this); } handleClick() { this.setState({ count: this.state.count + 1 }); } render() { return ( <div> <button onClick={this.handleClick}>Click me</button> <p>You clicked {this.state.count} times</p> </div> ); } } export default ClickCounter;

Common Events in React:

Event NameDescription
onClickTriggered when an element is clicked.
onChangeTriggered when the value of an input changes.
onSubmitTriggered when a form is submitted.
onMouseEnterTriggered when the mouse enters an element.
onMouseLeaveTriggered when the mouse leaves an element.
onKeyDownTriggered when a key is pressed down.
onKeyUpTriggered when a key is released.
onFocusTriggered when an element gains focus.
onBlurTriggered when an element loses focus.

React's event system is designed to be intuitive, efficient, and cross-browser compatible, making it easier to build interactive UIs.

Comments

Popular posts from this blog

What is Arrow Function?

 An arrow function is a concise way to write functions in JavaScript, introduced in ES6 (ECMAScript 2015). It uses the => syntax and is often preferred for its simplicity and ability to maintain the this context of the surrounding code. Syntax (param1, param2, ..., paramN) => expression If the function body contains multiple statements, you need to wrap it in curly braces {} and use the return keyword explicitly: (param1, param2, ..., paramN) => { // multiple statements return result; } If there is only one parameter, you can omit the parentheses: param => expression If there are no parameters, you must use empty parentheses: () => expression Examples Single-line arrow function: const add = ( a, b ) => a + b; console . log ( add ( 2 , 3 )); // Output: 5 Multi-line arrow function: const multiply = ( a, b ) => { const result = a * b; return result; }; console . log ( multiply ( 2 , 3 )); // Output: 6 Arrow function with one paramete...

React Interview Questions and Answers

 React is an efficient, flexible, and open-source JavaScript library designed to help developers create simple, fast, and scalable web applications. It was created by Jordan Walke, a software engineer at Facebook, and was first deployed on Facebook’s News Feed in 2011 and Instagram in 2012. React's simplicity and effectiveness make it accessible for developers with a JavaScript background to quickly build powerful web applications. Today, React is widely used by leading tech companies, including Facebook, Dropbox, Instagram, WhatsApp, Atlassian, and Meta. Its popularity stems from powerful features like the Virtual DOM, Components, State and Props, JSX, Hooks, and Routing, which simplify and enhance web development. To secure developer roles at top companies using React, it's essential to master the framework and prepare thoroughly. Familiarizing yourself with these top React interview questions can help you stand out as an expert in front of interviewers. 1. What is React.js? ...