我是TypeScript的新手。我一直遇到两个具体的错误,希望从愿意提供帮助的人中得到一些澄清。我正在大量使用Material UI构建一个React应用程序。我直接从MUI的文档中复制了示例代码:
https://material-ui.com/components/selects/
(特别是Customized Selects源代码,TypeScript选项)
当我粘贴到vs代码中时,出现大量错误。有问题的两个是:
Cannot find name 'div'. ts(2304)
'FormControl' refers to a value, but is being used as a type here. ts(2749)
任何帮助将不胜感激!
MUI源代码:
import React from 'react';
import { createStyles, makeStyles, withStyles, Theme } from '@material-ui/core/styles';
import InputLabel from '@material-ui/core/InputLabel';
import MenuItem from '@material-ui/core/MenuItem';
import FormControl from '@material-ui/core/FormControl';
import Select from '@material-ui/core/Select';
import NativeSelect from '@material-ui/core/NativeSelect';
import InputBase from '@material-ui/core/InputBase';
const BootstrapInput = withStyles((theme: Theme) =>
createStyles({
root: {
'label + &': {
marginTop: theme.spacing(3),
},
},
input: {
borderRadius: 4,
position: 'relative',
backgroundColor: theme.palette.background.paper,
border: '1px solid #ced4da',
fontSize: 16,
padding: '10px 26px 10px 12px',
transition: theme.transitions.create(['border-color', 'box-shadow']),
// Use the system font instead of the default Roboto font.
fontFamily: [
'-apple-system',
'BlinkMacSystemFont',
'"Segoe UI"',
'Roboto',
'"Helvetica Neue"',
'Arial',
'sans-serif',
'"Apple Color Emoji"',
'"Segoe UI Emoji"',
'"Segoe UI Symbol"',
].join(','),
'&:focus': {
borderRadius: 4,
borderColor: '#80bdff',
boxShadow: '0 0 0 0.2rem rgba(0,123,255,.25)',
},
},
}),
)(InputBase);
const useStyles = makeStyles((theme: Theme) =>
createStyles({
margin: {
margin: theme.spacing(1),
},
}),
);
export default function CustomizedSelects() {
const classes = useStyles();
const [age, setAge] = React.useState('');
const handleChange = (event: React.ChangeEvent<{ value: unknown }>) => {
setAge(event.target.value as string);
};
return (
<div>
<FormControl className={classes.margin}>
<InputLabel htmlFor="demo-customized-textbox">Age</InputLabel>
<BootstrapInput id="demo-customized-textbox" />
</FormControl>
<FormControl className={classes.margin}>
<InputLabel id="demo-customized-select-label">Age</InputLabel>
<Select
labelId="demo-customized-select-label"
id="demo-customized-select"
value={age}
onChange={handleChange}
input={<BootstrapInput />}
>
<MenuItem value="">
<em>None</em>
</MenuItem>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</Select>
</FormControl>
<FormControl className={classes.margin}>
<InputLabel htmlFor="demo-customized-select-native">Age</InputLabel>
<NativeSelect
id="demo-customized-select-native"
value={age}
onChange={handleChange}
input={<BootstrapInput />}
>
<option value="" />
<option value={10}>Ten</option>
<option value={20}>Twenty</option>
<option value={30}>Thirty</option>
</NativeSelect>
</FormControl>
</div>
);
}