javascript - React - semantic ui dropdown with object - Stack Overflow

I'm mapping over an array of objects and using the name in dropdown selection w semantic-ui-react

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 badges
Add a ment  | 

1 Answer 1

Reset to default 6

As 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
              }
            })}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745453277a4628369.html

相关推荐

  • javascript - React - semantic ui dropdown with object - Stack Overflow

    I'm mapping over an array of objects and using the name in dropdown selection w semantic-ui-react

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信