Languages
[Edit]
EN

React - Focus Events

0 points
Created by:
Zayaan-Rasmussen
543

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);

References

  1. blur event (js) - MDN Docs

  2. focus event (js) - MDN Docs

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

ReactJS - events

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join