I'm trying to map over an array of objects and use the name key to pass it to the options prop in react-select
. Can I just do this using regular JS? I'm trying to incorporate this example.
My mock data
mock.onGet("/dataschemas").reply(200, {
data: [
{
id: "2147483599",
selfUri: "/dataschemas/2147483599",
name: "Book Catalog"
},
{
id: "2147483600",
selfUri: "/dataschemas/2147483600",
name: "Business Articles"
},
{
id: "2147483602",
selfUri: "/dataschemas/2147483602",
name: "Phone Data"
}
]
});
In cDM
I'm updating state with the response and storing it in schemas
componentDidMount() {
axios.get("/dataschemas").then(function(response) {
console.log(response.data.data);
this.setState({
schemas: response.data.data
});
console.log(this.state.schemas);
});
}
Then in my select component I'm setting the schemas to the options
prop and mapping over that in the values
prop
<Select
id="color"
options={this.state.schemas}
isMulti={false}
value={this.state.schemas.filter(
({ name }) => name === this.state.name
)}
getOptionLabel={({ name }) => name}
getOptionValue={({ id }) => id}
onChange={({ value }) => this.setState({ name: value })}
onBlur={this.handleBlur}
/>
I can't seem to get the right values in props, to display the dataschema names in the dropdown selection in my codesandbox example
More info on react-select docs pertaining to this issue