Advertisement
noctual

Example using controlled Mui Select

Aug 2nd, 2022
1,615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // https://mui.com/material-ui/react-select/
  2. import React, { useState, useEffect } from 'react'
  3. import { FormControl, InputLabel, Select, MenuItem } from '@mui/material'
  4.  
  5. export default function App() {
  6.     const [age, setAge] = useState('')
  7.  
  8.     const handleChange = (event) => {
  9.         setAge(event.target.value)
  10.     }
  11.  
  12.     useEffect(() => {
  13.         // установить значение через секунду после загрузки страницы
  14.         setTimeout(() => {
  15.             setAge(10)
  16.         }, 1000)
  17.     }, [])
  18.  
  19.     return (
  20.         <FormControl fullWidth>
  21.             <InputLabel id="demo-simple-select-label">Age</InputLabel>
  22.             <Select
  23.                 labelId="demo-simple-select-label"
  24.                 id="demo-simple-select"
  25.                 value={age}
  26.                 label="Age"
  27.                 onChange={handleChange}
  28.             >
  29.                 <MenuItem value={10}>Ten</MenuItem>
  30.                 <MenuItem value={20}>Twenty</MenuItem>
  31.                 <MenuItem value={30}>Thirty</MenuItem>
  32.             </Select>
  33.         </FormControl>
  34.     )
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement