Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Show the name of the menu item when I select it with react hooks

So i have on my parent component some data like this:

const filters =[
name1,
name2,
name3,
etc

]

and I try to make a Select button with a list (using mui/material) from that data that i get on my component using props. I actually made the list while I map through the data like my code below. But the problem is that i want when I select an item the name to be shown on the select button and change every time I select a diffrent one. Thats why used useState to change the value every time but im not sure how can i make a function to get each name from the data, becaue I use the map function after the select component.

import * as React from 'react';
import { Button, Menu, MenuItem } from '@mui/material';
import { FormControl, FormHelperText, Select } from '@mui/material';

import { useEffect, useState } from 'react';

const SampleMenuList = (props) => {
    
    const [checked, setChecked] = useState(0);
    const [anchorEl, setAnchorEl] = React.useState(null);

    const [value, setValue] = useState(''); //here im setting the state 


    const open = Boolean(anchorEl);

    const handleToggle = (value) => { 
        setAnchorEl(null);
        if (value !== checked) {
            setChecked(value);
            props.onFilterChange(props.filters[value]);
        }
        
    };

    const handleSelectChange = (event) => {
        setValue() // here im not sure how can i get the name from the data when i choose is menu item
    };

    return (
        

        <Select value={value} onChange={handleSelectChange}>
            {props.filters.map((c, idx) => {
                const labelId = `checkbox-list-label-${idx}`;
                return (
                    <MenuItem
                        onClick={handleToggle.bind(this, idx)}
                        key={idx}
                        id={labelId}
                        value={c}
                    >
                        {c}
                    </MenuItem>
                );
            })}
        </Select>
    );
};

export default SampleMenuList;



MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can get the value of selected item from event object.

More about Select in Mui

Example: (replaces handleSelectChange event)

const handleSelectChange = (event) => {
  setValue(event.target.value);
};

Hope this will help!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading