How to get selected value from mui select in react js?

Hi Friends ๐Ÿ‘‹,

Welcome To aGuideHub!

To get selected value from mui select in React js, you can use mui select onchange attribute, you have to set their value in your state. It will get the selected value of mui select.

Today, I am going to show you, how to get selected value from mui select in react js.

Installation

Install the following packages to use get selected value from mui select in react js.

npm

npm install @mui/material @emotion/react @emotion/styled

yarn

yarn add @mui/material @emotion/react @emotion/styled

MUI material get selected value from mui select example.

The below code is an example of a Material UI select. You have to import @mui material select and set the value in the state using on change attribute and show the state valve in the <p> tag thatโ€™s how you can get the selected value and use it wherever you want.

App.js

import * as React from 'react';
import Box from '@mui/material/Box';
import InputLabel from '@mui/material/InputLabel';
import MenuItem from '@mui/material/MenuItem';
import FormControl from '@mui/material/FormControl';
import Select from '@mui/material/Select';

export default function BasicSelect() {
  const [age, setAge] = React.useState('');

  const handleChange = (event) => {
    setAge(event.target.value);
  };

  return (
    <Box sx={{ minWidth: 120 }}>
      <FormControl>
        <InputLabel id="demo-simple-select-label">Age</InputLabel>
        <Select
          labelId="demo-simple-select-label"
          id="demo-simple-select"
          value={age}
          label="Age"
          onChange={handleChange}
        >
          <MenuItem value={10}>Ten</MenuItem>
          <MenuItem value={20}>Twenty</MenuItem>
          <MenuItem value={30}>Thirty</MenuItem>
        </Select>
      </FormControl>
      <p>Selected Value</p>
    <p>{age}</p>
    </Box>
  );
}

In the above code example, I have used the @mui/material component and got the selected value from mui select.

Check the output of the above code example.

React, Mui, get, selected, value

Here, we are provided code sandbox links for the above program get selected value from mui select. Then you can use whenever you want and do the changes as per your requirements.

Try it Yourself

All the best ๐Ÿ‘

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Interview Questions

Soon You will get CSS, JavaScript, React Js, and TypeScript So Subscribe to it.

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

Related Posts