EN
React - Focus Events
0
points
In this article, we would like to show you focus events in React.
There are two focus events:
- onFocus
- onBlur
Note:
focus events
work on all elements in the React DOM.
In the following example, we create functions that handle these events and display to the console which action was performed.
Runnable example:
// ONLINE-RUNNER:browser;
// Note: Uncomment import lines during working with JSX Compiler.
// import React from "react";
// import ReactDOM from "react-dom";
const App = () => {
const handleOnFocus = () => {
console.log('onFocus event was handled');
}
const handleOnBlur = () => {
console.log('onBlur event was handled');
}
return (
<div>
<input type="text"
onFocus={handleOnFocus}
onBlur={handleOnBlur}
placeholder="Click here"
/>
</div>
);
};
const root = document.querySelector('#root');
ReactDOM.render(<App />, root);