Event handling in React allows you to respond to user interactions like clicks, form submissions, and other events. Here’s a basic overview and example:
Here's a simple example of handling a button click and an input change:
import React, { useState } from 'react'; const EventHandlingExample = () => { const [inputValue, setInputValue] = useState(''); const handleClick = () => { alert(`Button clicked! Input value: ${inputValue}`); }; const handleChange = (event) => { setInputValue(event.target.value); }; return ( <div> <input type="text" value={inputValue} onChange={handleChange} placeholder="Type something..." /> <button onClick={handleClick}>Click Me</button> </div> ); }; export default EventHandlingExample;
Feel free to ask if you need specific examples or further explanations!
The above is the detailed content of Event Handling in React. For more information, please follow other related articles on the PHP Chinese website!