Can't figure out how to display selected value in 1.0 beta 13
I'm sure this is a newbie question, but I'm trying to use react-select 1.0 beta 13 with async calling a RESTful API to provide the list of values. I have successfully populated the list once I start to type in the select box from the restful API. When I select one of the values, I can log it to the console. What I can't do is get the selected value to display on the page. This code is largely lifted from the examples
import React from 'react';
import {render} from 'react-dom';
import Select from 'react-select';
import 'whatwg-fetch';
import 'react-select/dist/react-select.css';
const getOptions = (input) => {
return fetch(`http://localhost:5000/keywords/`+input,{method: "get"})
.then((response) => {
return response.json();
}).then((json) => {
return { options: json };
});
}
class App extends React.Component {
constructor(props) {
super(props);
this.onChange = this.onChange.bind(this);
this.state = { value: '' }
}
onChange(val) {
console.log('Setting value to ',val.label)
this.setState({value: val.label})
}
render() {
return (
<div>
<Select.Async
autoload={false}
name="testme"
value={this.state.value}
loadOptions={getOptions}
onChange={this.onChange}
/>
</div>
);
}
}
render(<App/>, document.getElementById('app'));
I'm tearing my hair out at this point trying to understand how to have the selected value displayed. I've seen a few issues identified regarding the ability to add new items to the list; I don't particularly need that at this point, I just want to display the selected value. Any help you can provide would be appreciated. And BTW my API doesn't like being called with no values; I do see a 404 error when the page first loads, so it doesn't look like the autoload={false} option is working either.
Unless I'm missing something, I can see nothing wrong with this at all except for the lack of rendering the value from the state, e.g.:
render() {
return (
<div>
<Select.Async
autoload={false}
name="testme"
value={this.state.value}
loadOptions={getOptions}
onChange={this.onChange}
/>
<div>The selected value is {this.state.value}.</div>
</div>
);
}
Thanks, I added that additional line, and now it once I select one of the values returned from the API, the sentence below the control does change. So, for example if I select "Add-on Hardware" from the drop-down list of choices, the sentence in the code addition you suggested says "The selected value is Add-on Hardware". So this gives me some confidence I've connected the API to the component correctly. However, the issue I'm trying to understand and address is the behavior of the input box (possibly the wrong term). In my code above, it does not contain the phrase "Add-on Hardware" that I selected, it still shows "Select..." in light gray as if I hadn't made a selection at all.
In the react-select async example Contributors in single-select mode, when you choose a contributor name, the input box (that when cleared shows a light grey "Select...") changes to display the name you selected (e.g. Dave Brotherstone). This is of course consistent with what a user would expect.
Having said that I'm using beta 13, that may not be exactly true. I cloned the repository yesterday, built the component, and so I'm using current source. I recognize that running the code above isn't possible, let me see if I can point my code at the Contributors example data source to see if I can make this more easily reproducible.
You're binding the value of the <select> to this.state.value, but when you set the state, you're setting the value to that of val.label. This won't match one of the values in the <select> (it'll match one of the labels, instead), so the user's selection won't be selected. Try setting the state's value to val.value instead. You can always assign the label to another variable in the state if you'd like to use it elsewhere, e.g.:
this.setState(
{
value: val.value,
label: val.label
}
);
Thanks, it had to be something that obvious, I really appreciate your help!
No problem!