I'm mapping over an array of objects and using the name in dropdown selection w/ semantic-ui-react.
I have some mock data
mock.onGet("/dataschemas").reply(200, {
data: [
{
id: "2147483599",
selfUri: "/dataschemas/2147483599",
name: "Book Catalog"
},
{
id: "2147483600",
selfUri: "/dataschemas/2147483600",
name: "Business Articles"
}
]
});
in cDM I'm updating state with the object as dataschemas
async ponentDidMount() {
await this.getSchemas();
}
getSchemas = async () => {
try {
const { data } = await axios.get("/dataschemas");
console.log(data);
const dataschemas = data.data;
this.setState({ dataschemas: dataschemas });
console.log("This is the dataschema list:", dataschemas);
} catch (error) {
console.error(Error(`Error fetching results list: ${error.message}`));
}
};
My change handler function looks like this:
handleSchemaChange = (e, { value }) => this.setState({ name: value });
Then in render I'm using the <Dropdown>
ponent
render() {
const { dataschemas } = this.state;
return (
<div>
<div>
<label>Select a dataschema: </label>
<Dropdown
placeholder="Select data schema"
clearable
fluid
selection
multiple={true}
options={dataschemas}
header="PLEASE SELECT A DATASCHEMA"
value={dataschemas.filter(({ name }) => name === this.state.name)}
onChange={this.handleSchemaChange}
/>
</div>
</div>
);
}
I can't get the dataschema names
to show in the dropdown or get label to update when a selection is made. I don't know if I'm missing a prop or not updating the state correctly, any ideas?
Codesandbox here
I'm mapping over an array of objects and using the name in dropdown selection w/ semantic-ui-react.
I have some mock data
mock.onGet("/dataschemas").reply(200, {
data: [
{
id: "2147483599",
selfUri: "/dataschemas/2147483599",
name: "Book Catalog"
},
{
id: "2147483600",
selfUri: "/dataschemas/2147483600",
name: "Business Articles"
}
]
});
in cDM I'm updating state with the object as dataschemas
async ponentDidMount() {
await this.getSchemas();
}
getSchemas = async () => {
try {
const { data } = await axios.get("/dataschemas");
console.log(data);
const dataschemas = data.data;
this.setState({ dataschemas: dataschemas });
console.log("This is the dataschema list:", dataschemas);
} catch (error) {
console.error(Error(`Error fetching results list: ${error.message}`));
}
};
My change handler function looks like this:
handleSchemaChange = (e, { value }) => this.setState({ name: value });
Then in render I'm using the <Dropdown>
ponent
render() {
const { dataschemas } = this.state;
return (
<div>
<div>
<label>Select a dataschema: </label>
<Dropdown
placeholder="Select data schema"
clearable
fluid
selection
multiple={true}
options={dataschemas}
header="PLEASE SELECT A DATASCHEMA"
value={dataschemas.filter(({ name }) => name === this.state.name)}
onChange={this.handleSchemaChange}
/>
</div>
</div>
);
}
I can't get the dataschema names
to show in the dropdown or get label to update when a selection is made. I don't know if I'm missing a prop or not updating the state correctly, any ideas?
Codesandbox here
Share Improve this question edited Mar 14, 2019 at 21:11 DJ2 asked Mar 14, 2019 at 21:05 DJ2DJ2 1,7513 gold badges37 silver badges76 bronze badges1 Answer
Reset to default 6As specified in https://react.semantic-ui./modules/dropdown/ you should use the following structure to display an object inside a Dropdown.
{
"key": "",
"text": "",
"value": ""
}
Example: use this as options value in your Dropdown
options={dataschemas.map(ds => {
return {
key: ds.id,
text: ds.name,
value: ds.selfUri
}
})}