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.

Include `name` property in `onChange(selected)`

See original GitHub issue

This would make it easier to use a single onChange handler for tracking current state.

e.g. The same change handler

const onChange = (selected) => {
  this.setState({ [selected.name]: selected.value })
}

Could be used for

<Select
    name="form-field-name-one"
    value="one"
    options={optionsForOne}
    onChange={onChange}
  />

<Select
    name="form-field-name-two"
    value="two"
    options={optionsForTwo}
    onChange={onChange}
  />

My current workaround is to create a handler which uses a closure to track the name.

_createChangeHandler(name) {
  const onChange = (newValue) => this.setState({ [name]: newValue.value });
  return onChange;
}

const onChange = this._createChangeHandler(name);
return (
  <Select
    name={name}
    value={this.state[name]}
    onChange={onChange}
    ...
  />
);

but I’d prefer it be available from the function’s argument(s).

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:11
  • Comments:11

github_iconTop GitHub Comments

10reactions
1-800-jonocommented, Jul 26, 2017

It seems weird that there’s no option to retrieve the name of the Select. How are people dealing with multiple Selects in forms? Multiple onChange functions…?

7reactions
rhysstubbscommented, Nov 27, 2018

The fact this functionality hasn’t been implemented is enough for me to switch to a different solution, is there any hope this will change? Will pull-requests be accepted for it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

when using {react-select} Cannot read property 'name' of ...
The first argument is the selected option (or options if you have isMulti set). There is also a second argument which is an...
Read more >
View or change the properties for an Office file
View or create custom properties for the current file · In the Name box, type a name for the custom property, or select...
Read more >
SCR19: Using an onchange event on a select element ... - W3C
The objective of this technique is to demonstrate how to correctly use an onchange event with a select element to update other elements...
Read more >
onchange Event - W3Schools
The onchange event occurs when the value of an element has been changed. For radiobuttons and checkboxes, the onchange event occurs when the...
Read more >
Function.prototype.name - JavaScript - MDN Web Docs
To change it, use Object.defineProperty() . The name property is typically inferred from how the function is defined. In the following sections, ...
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