Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

211
Views
How Can I Dynamically Create Menu Items in MIU Select Component?

I am trying to use the map method on a list of items (hours) to dynamically create Menu Items in an MUI Select component. I am having trouble getting the clicked on item in the custom component (SelectHours) which would give me the current time.

Here is my App.js:

import logo from './logo.svg';
import './App.css';
import React from 'react'
import SelectHours from './SelectHours/SelectHours.js'
import { Select, MenuItem, Menu } from '@mui/material'
import { useState } from 'react';

class App extends React.Component {

    handleChange(e) {
        console.log("handleChange: ", e)
    }

    constructor(props) {
        super(props);
        console.log("props", props)
        this.input = React.createRef();
        var hoursArray = ['0000', '0100', '0200', '0300', '0400', '0500', '0600', '0700', '0800', '0900', '1000', '1100', '1200', '1300', '1400', '1500', '1600', '1700', '1800', '1900', '2000', '2100', '2200', '2300'];
        this.state = { value: "[]", start: hoursArray[5], end: "2100", hoursArray: hoursArray, items: [{ sup: "sup1" }, { sup2: "sup2" }, { sup: "sup3" }]}
    }

    render() {
        return (
            <div className="TopContainer">
            <Select
            labelId="select-demo"
            id='select-demo'
            onChange={event => this.handleChange(event.target.value)}
            >
            <SelectHours>
            </SelectHours>
        </Select>
        </div>
    );
  }
}

export default App

The SelectHours component is supposed to dynamically create Menu Items on a list using .map, but I am not getting the selected index with onClick:

import React, { useState } from 'react';
import { Select, MenuItem, Menu } from '@mui/material'

function SelectHours() {
    var hoursArray = ['0000', '0100', '0200', '0300', '0400', '0500', '0600', '0700', '0800', '0900', '1000', '1100', '1200', '1300', '1400', '1500', '1600', '1700', '1800', '1900', '2000', '2100', '2200', '2300']

function setHour(hour) {
    console.log("foo")
}

return hoursArray.map(hour => {
    return (
        <MenuItem
            value={hour}
            key={hour}
            onClick={setHour(hour)}
        >
            <label>
                {hour}
            </label>
            </MenuItem>
    )
    })
}

export default SelectHours

Can anyone tell me how to grab that hour string?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!