question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting value onBlur

See original GitHub issue

Is it possible to get the value of select on blur? Unlike onChange onBlur returns an event. So when I do something like e.target.value I always get an empty string.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:8
  • Comments:5

github_iconTop GitHub Comments

2reactions
arun678commented, Jun 11, 2020

👍 for adding this.

would be nice if the first argument passed to the onChange callback was a similar event rather than just the value.

P.S This issue was killing me today in conjunction with #188, as I want to trigger validation after the initial blur event, then on subsequent change events.

If anyone else is in a similar situation, I ended up resolving it with this wrapper component. The faux event objects are naive, but I only need e.type and e.target.value

'use strict';

var React = require('react');
var Select = require('react-select');

module.exports = React.createClass({

    displayName: 'SelectWrapper',

    getDefaultProps () {
        return {
            value: null
        }
    },

    getInitialState () {
        return {
            value: this.props.value
        }
    },

    onBlur () {
        let blurHandler = this.props.onBlur;
        if (blurHandler) {
            blurHandler({
                type: 'blur',
                target: {
                    value: this.state.value
                }
            })
        }
    },

    onChange (value) {

        let changeHandler = this.props.onChange;
        if (changeHandler) {
            changeHandler({
                type: 'change',
                target: {
                    value: value
                }
            });
        }

        this.setState({ value: value });
    },

    render () {
        let { onBlur, onChange, value, ...props } = this.props;

        return (
            <Select onBlur={ this.onBlur } onChange={ this.onChange } { ...props } { ...this.state } />
        );
    }

});

You might want to slightly change this. OnBlur can trigger very quickly after onChange. setState is async call. So if you are calling changeHandler and then setState, there’s a good chance that you might have not received the value you have set in state for onBlur event. Try this scenario

  1. Select the option
  2. OnChange triggers and ChangeHandler() is called (state value is null)
  3. The selected value is returned through your ChangeHandler() but state value is still null.
  4. Setstate is called to assign the selected option to state (no guarantee on state value to be set immediately since it is asynchronous)
  5. OnBlur is triggered immediately (if you are not using ‘blurInputOnSelect’ prop to keep the focus still)
  6. At this point state value might have not been set yet (state value still could be null)

But here you were expecting the state value which was set by OnChange

So you could first call setState and use the callback to trigger OnChangeHandler

onChange = (e) => {
        const { value } = e
        let changeHandler = this.props.clickHandler;
        this.setState({ value: value }, () => {
            if (changeHandler) {
                changeHandler({
                    value: value,
                    name: this.props.name
                });
            }
        });
    }
    onBlur = () => {
        const { value } = this.state;
        let blurHandler = this.props.blurHandler;
        if (blurHandler) {
            blurHandler({
                value: value,
                name: this.props.name,
            })
        }
    }
2reactions
dh376commented, Sep 6, 2016

+1

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get value onblur from input fields in a loop - Stack Overflow
How would I get the value too? Normally I would just set a variable like usual but it's in a loop so I'm...
Read more >
onblur Event - W3Schools
The onblur event occurs when an element loses focus. The onblur event is often used on input fields. The onblur event is often...
Read more >
Focusing: focus/blur - The Modern JavaScript Tutorial
An element receives the focus when the user either clicks on it or uses the Tab key on the keyboard. There's also an...
Read more >
Examples on How onblur Event Works in JavaScript - eduCBA
onblur is an in-built event that is available in JavaScript which gets triggered when the elements on which it is applied loses its...
Read more >
.blur() | jQuery API Documentation
Description: Bind an event handler to the "blur" JavaScript event, or trigger that event on an element. ... <input id="target" type="text" value="Field 1">....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found