Certainly! React allows you to handle events in a similar way to standard HTML elements but with a more declarative syntax. Here's an example demonstrating how to handle events in React:
import React, { useState } from 'react'; function EventHandling() { const [buttonText, setButtonText] = useState('Click me'); // Event handler function const handleClick = () => { setButtonText('Button clicked'); }; return ( <div> <button onClick={handleClick}>{buttonText}</button> </div> ); } export default EventHandling;
In this example:
EventHandling
.useState
hook to create a state variable buttonText
and a function setButtonText
to update its value.handleClick
that updates the buttonText
state when the button is clicked.handleClick
function to the onClick
event of the button using JSX.This demonstrates a basic example of event handling in React. You can handle various events such as onClick
, onChange
, onSubmit
, etc., using similar syntax.