How can I get dropdown options populated from state?

Alex

I can't seem to get a select element to have options based on elements of an array in state. I've tried a bunch of different methods, but this seemed to be the most granular (I'm just getting back into react after a few years, and am trying to relearn it).

I have a whole stateful component for the dropdown, shown below:

import React, { Component } from "react";
import "./Dropdown.css";


class Dropdown extends Component {
  constructor(props) {
    super(props);
    this.state = {
      options: [],
      selectedOption: "",
    };
  };

  componentDidMount = () => {
    this.setState({
      options: this.props.options
    });
  };

  displayOptions = () => {
    this.state.options.map(item => {
      return <option value={item} id="1">{item}</option>
    });
  };

  render() {
    return(
      <select className="dropdown">
        { this.displayOptions() }
      </select>
    )
  };
};

export default Dropdown;

The props that are being pulled in are structured as an array of either strings or numbers. As an example:

[2020,2019,2018,2017,2016,2015,2014,2013,2012,2011,2010,2009,2008,2007,2006,2005,2004,2003,2002,2001,2000,1999,1998,1997,1996,1995,1994,1993,1992]

Looking at my React dev tools, state is getting correctly populated with the array. And according to my understanding of React, once state is populated, the component should rerender, calling displayOptions, mapping through the array, and filling the select element with option elements.

But when I go to the actual page, no matter how long I wait or what I do, the dropdown never opens to show options. I guess I can't tell if I just have a typo that's keeping this from working properly, or if I'm missing a larger rule of either React, array prototypes, or something else entirely.

Any help would be greatly appreciated. And please let me know if more information is needed. I feel like I've provided all relevant info, but I'm happy to provide more if needed.

Manny

return from the function

 displayOptions = () => {
    return this.state.options.map(item => {
      return <option value={item} id="1">{item}</option>
    });
  };

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I get a populated combobox to display its options in Swift?

From Dev

How to get a value from a self populated dropdown using knockout?

From Dev

How to get a value from a self populated dropdown using knockout?

From Dev

How can I get stateId from cascading dropdown List

From Dev

How can I get US State from GeoIP2?

From Java

How can I get the state from `componentDidMount `(using Redux)?

From Dev

How can I return all the options of the select dropdown?

From Dev

How can I get my compiled array from ng-options in a custom directive?

From Dev

Chosen dropdown search is not working for dynamically populated options

From Dev

PhantomJS does not see dropdown options populated by AJAX

From Dev

How can i get text option from AJAX generated select dropdown using jQuery?

From Dev

How to convert data from two tables get from Linq query to List witch can be populated in wpf form

From Dev

How to get information from an already populated form

From Dev

How can I stop my RecyclerView, populated from Firebase Database and managed by FirebaseRecyclerAdapter, from recreating deleted views?

From Dev

PHP dropdown populated from database

From Dev

How can I prevent the "Attempting to get the view from an adapter in state TextDocDataAvailable" fail in visual Studio 2013?

From Java

How can I pull a specific object from the state and get one of its items?

From Dev

How can I get "data payload" from push notification (using FCM) when android app is in killed state?

From Dev

How can I get a variable from a JavaScript promises (python calls), avoiding the pending state in Odoo?

From Dev

How can I get records which created by specific license id in populated object?

From Dev

How can I change the datatype of a populated table column from int to UNIQUEIDENTIFIER?

From Dev

How can I load checkboxes with dynamic names in a listbox, the names are populated from an XML having a <NAME> tag

From Dev

How can I get this switch to pick just one of the 3 options?

From Dev

In multi dropdown , How can i get the currently removed value

From Dev

how can i get the selected value of dropdown list in php?

From Dev

How can I get the dropdown menu to appear on top of the dialog?

From Dev

How can I get selected value on the dropdown with vue.js?

From Dev

How can I filter text/divs from two select options?

From Dev

How can I remove the Documents and Pictures folders from the Indexing options?

Related Related

  1. 1

    How can I get a populated combobox to display its options in Swift?

  2. 2

    How to get a value from a self populated dropdown using knockout?

  3. 3

    How to get a value from a self populated dropdown using knockout?

  4. 4

    How can I get stateId from cascading dropdown List

  5. 5

    How can I get US State from GeoIP2?

  6. 6

    How can I get the state from `componentDidMount `(using Redux)?

  7. 7

    How can I return all the options of the select dropdown?

  8. 8

    How can I get my compiled array from ng-options in a custom directive?

  9. 9

    Chosen dropdown search is not working for dynamically populated options

  10. 10

    PhantomJS does not see dropdown options populated by AJAX

  11. 11

    How can i get text option from AJAX generated select dropdown using jQuery?

  12. 12

    How to convert data from two tables get from Linq query to List witch can be populated in wpf form

  13. 13

    How to get information from an already populated form

  14. 14

    How can I stop my RecyclerView, populated from Firebase Database and managed by FirebaseRecyclerAdapter, from recreating deleted views?

  15. 15

    PHP dropdown populated from database

  16. 16

    How can I prevent the "Attempting to get the view from an adapter in state TextDocDataAvailable" fail in visual Studio 2013?

  17. 17

    How can I pull a specific object from the state and get one of its items?

  18. 18

    How can I get "data payload" from push notification (using FCM) when android app is in killed state?

  19. 19

    How can I get a variable from a JavaScript promises (python calls), avoiding the pending state in Odoo?

  20. 20

    How can I get records which created by specific license id in populated object?

  21. 21

    How can I change the datatype of a populated table column from int to UNIQUEIDENTIFIER?

  22. 22

    How can I load checkboxes with dynamic names in a listbox, the names are populated from an XML having a <NAME> tag

  23. 23

    How can I get this switch to pick just one of the 3 options?

  24. 24

    In multi dropdown , How can i get the currently removed value

  25. 25

    how can i get the selected value of dropdown list in php?

  26. 26

    How can I get the dropdown menu to appear on top of the dialog?

  27. 27

    How can I get selected value on the dropdown with vue.js?

  28. 28

    How can I filter text/divs from two select options?

  29. 29

    How can I remove the Documents and Pictures folders from the Indexing options?

HotTag

Archive