Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJS - How to get onFocus and onBlur to work

As I understand it, onFocus should be called when the input box is clicked into, and onBlur should be called when something else becomes the focus.

My intentions: I would like to call a function that, when activated by a click, it will .concat the message string of the input box in focuse, but I can't get the onFocus or onBlur to work.

From what I've found searching around, this should do the trick, but doesn't.

import React, { Component } from 'react'
class SocialPost extends Component {
    state = {
        message: this.props.socialPost.message,
        focus: false
    }
    _onBlur() {
        setTimeout(() => {
            if (this.state.focus) {
                this.setState({
                    focus: false,
                });
            }
        }, 0);
    }
    _onFocus() {
        if (!this.state.focus) {
            this.setState({
                focus: true,
            });
        }
    }
    render() {
        return (
            <div className='...'>
                <div className='...'>
                    ...
                    <input
                        onFocus={this._onFocus()}
                        onBlur={this._onBlur()}
                        type='text'
                        value={this.state.message}
                        onChange={...}/>
                    ...
                </div>
            </div>
        )
    }
}
export default SocialPost
  1. Why is onFocus and onBlur called when the component is rendered before I click/unclick them?
  2. How do I get this onFocus and onBlur to work, is is there another way to focus on the activated input box?
like image 694
Kevin Danikowski Avatar asked Oct 23 '17 19:10

Kevin Danikowski


People also ask

How do you use onFocus and onBlur in React?

Use the React. FocusEvent<HTMLElement> type to type the onFocus and onBlur events in React. The FocusEvent interface is used for onFocus and onBlur events.

Why is onBlur not fired?

You can add tabIndex={0} to outermost div in order to dismiss keyboard from input. If the element that has the onBlur effect and tabindex is created onClick of another element, it does not automatically gets focus when it appears. Thus, you may need to focus it using element. focus() after creating the element.

How do you use onBlur in React JS?

onBlur triggers when focus is lost from the input element in context. In React. js, we bind event handlers by passing a method defined in the component to be called accordingly. .bind(this) is called in order to retain the value of this and expose component methods within the event handler function such as this.

How do you use onFocus event in React?

In React, the onFocus event is called when the element receives focus and onBlur event is called when focus has left the element. There are 4 types of native focus events, focus/blur which do not bubble and focusin/focusout which bubble.


2 Answers

  1. It is called because you run the function in the render method, you should pass function, not what they return.

Change this:

onFocus={this._onFocus()}
onBlur={this._onBlur()}

to this:

onFocus={this._onFocus}
onBlur={this._onBlur}

2.You cannot declare component's state like that. It has to be done in constructor. Also if you want to refer to SocialPost's this you have to bind it. Imo the best place to do it is in the constructor. The whole code should look like this:

import React, { Component } from 'react'
class SocialPost extends Component {
    constructor (props) {
      super(props)
      this.state = {
        message: props.socialPost.message,
        focus: false
      }

      this._onBlur = this._onBlur.bind(this)
      this._onFocus = this._onFocus.bind(this)
    }
    _onBlur() {
        setTimeout(() => {
            if (this.state.focus) {
                this.setState({
                    focus: false,
                });
            }
        }, 0);
    }
    _onFocus() {
        if (!this.state.focus) {
            this.setState({
                focus: true,
            });
        }
    }
    render() {
        return (
            <div className='...'>
                <div className='...'>
                    ...
                    <input
                        onFocus={this._onFocus}
                        onBlur={this._onBlur}
                        type='text'
                        value={this.state.message}
                        onChange={...}/>
                    ...
                </div>
            </div>
        )
    }
}
export default SocialPost
like image 89
P. Zietal Avatar answered Sep 20 '22 03:09

P. Zietal


Change

onFocus={this._onFocus()}
onBlur={this._onBlur()}

to

onFocus={this._onFocus}
onBlur={this._onBlur}

or

onFocus={this._onFocus.bind(this)}
onBlur={this._onBlur.bind(this)}
like image 35
Alexander Vitanov Avatar answered Sep 21 '22 03:09

Alexander Vitanov