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.

Pass event instead of value for `onChange`

See original GitHub issue

It would be a lot more useful to pass an event object instead of simply the values when using the onChange prop. By just returning the value(s), there is no way to truly know what Typeahead called it originally, which makes implementing multiple Typaheads in a single form much more difficult (or at least convoluted). To make this functional now, I have to do the following:

The JSX from an overall larger form

<ControlLabel>List1</ControlLabel>
<Typeahead clearButton
        multiple
        type='text'
        name='item1'
        value={this.state.item1}
        onChange={this.handleItem1Change}
        options={this.state.list1}
/>

<ControlLabel>List2</ControlLabel>
<Typeahead clearButton
        multiple
        type='text'
        name='item2'
        value={this.state.item2}
        onChange={this.handleItem2Change}
        options={this.state.list2}
/>

These are the handlers I have to write.

    handleItem1Change(value) {
        this.handleTypeAheadChange('item1', value);
    }

    handleItem2Change(value) {
        this.handleTypeAheadChange('item2', value);
    }

    handleTypeAheadChange(name, values) {
        this.setState({
            [name]: values
        });
    }

This begs for a single handleChange function much like the example shown in React’s Controlled Component Form. Having to do this workaround doesn’t make any sense, but the way Typeahead’s work right now there doesn’t seem to be any other way that I could find. If there is a much simpler solution I would love to hear about it.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

10reactions
rkayaithcommented, Jun 23, 2017

Here’s a simpler way:

    handleTypeAheadChange = name => values => {
        this.setState({
            [name]: values
        });
    }

    <Typeahead
        onChange={this.handleTypeAheadChange('item1')}
        ...
    />
    <Typeahead
        onChange={this.handleTypeAheadChange('item2')}
        ...
    />
5reactions
ericgiocommented, Apr 4, 2018

As of v3.0.0, onInputChange passes the input’s change event as the second param:

<Typeahead
  ...
  onInputChange={(text, event) => {
    ...
  }}
/>
Read more comments on GitHub >

github_iconTop Results From Across the Web

React OnChange pass ID or Event - Stack Overflow
I have tried a bunch of different things but i cant return the event from onChange because it only passes the value.
Read more >
Handling Events - React
React events are named using camelCase, rather than lowercase. With JSX you pass a function as the event handler, rather than a string....
Read more >
React onChange Events (With Examples) - Upmostly
The onChange event in React detects when the value of an input element changes. Let's dive into some common examples of how to...
Read more >
HTML onchange Event Attribute - W3Schools
The onchange attribute fires the moment when the value of the element is changed. Tip: This event is similar to the oninput event....
Read more >
Onchange Event in JavaScript - Scaler Topics
In these examples, we are associating a function with the onchange event attribute. When the element's value is updated, and the element loses ......
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