Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly catch change/focusOut event on text input in React.js?

I've got a form, in which I want to handle change event on text inputs, but React onChange triggering on key down (opposite to native JS, that triggers change event when an input field is out of focus).

Is there a React way to do what I want?

like image 855
Alex Suslyakov Avatar asked Jun 03 '16 08:06

Alex Suslyakov


People also ask

How do you focus out from textbox in React JS?

To handle focus out in React, we use 'onFocusOut'. It is passed as an attribute in <input> elements, and can be used to perform actions when the cursor leaves the input box. React's onFocusOut event is related to the keyboard focus.

Why does the input field lose focus after typing a character React?

it is because you are rendering the form in a function inside render(). Every time your state/prop change, the function returns a new form. it caused you to lose focus.

How do you use onMouseOut in React?

Full example import React, { MouseEvent } from 'react'; const ButtonComponent = () => { const handleMouseEvent = (e: MouseEvent<HTMLButtonElement>) => { e. preventDefault(); // Do something }; return <button onMouseOut={handleMouseEvent}>Click me!


1 Answers

If you want to only trigger validation when the input looses focus you can use onBlur.

React uses onFocus and onBlur instead of onFocusIn and onFocusOut. All React events are normalized to bubble, so onFocusIn and onFocusOut are not needed/supported by React. 2

like image 68
Dominic Avatar answered Sep 17 '22 15:09

Dominic