1

I have a react component as follows

export class Foo extends Component {
  constructor(props) {
     super(props);
     this.getItems = this.getItems.bind(this);
     this.state = {
       items : []
     }
  }

  getItems() {
    // axios logic which  sets state.items to array [ "item1", "item2", "item3" ]
     axios.get('...backend', { headers: API_HEADERS })
    .then(response => {
        const items = response.data.Items
        this.setState({items});
    }).catch((error) => {
        console.log(error);
    });
  }
  componentDidMount() {
     this.getItems();
  }
  createDropdown() {
    return (
    <DropdownButton
    bsStyle='primary'
    title='Items'
    onSelect={this.handleSelect.bind(this)}
    >
    {this.state.items.map((item, index) => {
         <MenuItem key={index} value={item} eventKey={index}>{item}</MenuItem>
     })}
    </DropdownButton>
    )
  }
  render() {
    const items = this.createDropdown();
    return (
      ... Grid's, columns and the likes
      {items}
    )
  }
}

When rendering the page the react-boostrap DropdownButton looks as follows DropdropButton react-boostrap

I'm wondering what i'm doing wrong here. Why does the Dropdown not create MenuItem's accordingly to the length of the array?

I don't know whether I'm missing something. Thanks in advance

5
  • I don't see the problem based off the screenshot. Isn't the button supposed to expand and show the items after you click it?
    – izb
    Commented Sep 26, 2018 at 16:55
  • 1
    The button is clicked... The <MenuItem>'s do not render. If you look closely you can see a grey line indicating I have clicked it. It's like the box for the MenuItems appears but no content is shown..
    – N. Dury
    Commented Sep 26, 2018 at 16:59
  • Could you post your axios logic?
    – izb
    Commented Sep 26, 2018 at 17:17
  • Please see updated question. I have added the axios logic
    – N. Dury
    Commented Sep 26, 2018 at 17:19
  • I don't know if it's worth mentioning the render() method as <FormGroup> tags along wit the Grid & some rows and columns..
    – N. Dury
    Commented Sep 26, 2018 at 17:21

1 Answer 1

1

You are not returning MenuItem in your map. Update your code to return MenuItem.

{this.state.items.map((item, index) => {
         return <MenuItem key={index} value={item} eventKey={index}>{item}</MenuItem>
     })}

OR

{this.state.items.map((item, index) => (
    <MenuItem key={index} value={item} eventKey={index}>{item}</MenuItem>)
)}
2
  • This has solved my issue, I don't know what to say other than much obliged.
    – N. Dury
    Commented Sep 26, 2018 at 17:27
  • @N.Dury, no problem. Added one more method please check. So basically some value must be returned in map. Commented Sep 26, 2018 at 17:30

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.