gpt4 book ai didi

javascript - 如何使用 Typescript 实现 Material-UI ThemeProvider 和 WithStyles

转载 作者:行者123 更新时间:2023-12-02 21:29:37 26 4
gpt4 key购买 nike

我越来越沮丧,因为过去几天我一直试图将我的 React 应用程序从 javascript 迁移到 tsx。到目前为止,我喜欢 tsx 的类型检查,但我不相信 Material-ui 库,尽管它解释了如何通过 TypeScript 使用其样式。

我已按照此处的指南 https://material-ui.com/guides/typescript/ 进行操作认真地创建了一个主题:

import { createMuiTheme } from "@material-ui/core/styles";

export default function theme() {
return createMuiTheme({
palette: {
primary: {
main: "#607D8B",
light: "#CFD8DC",
dark: "#455A64",
contrastText: "#FFFFFF"
},
secondary: {
main: "#7C4DFF",
contrastText: "#212121"
}
}
})};

我创建了一种样式:

import { green, red } from "@material-ui/core/colors";
import { createStyles, withStyles } from "@material-ui/core/styles";
import { Theme } from '@material-ui/core';

const useStyles = (theme: Theme) => createStyles({
root: {
height: "100vh"
},
success: {
backgroundColor: green[600]
},
error: {
backgroundColor: red[800]
},
icon: {
fontSize: 20
},
iconVariant: {
opacity: 0.9,
marginRight: theme.spacing(1)
},
message: {
display: "flex",
alignItems: "center"
},
image: {
backgroundImage: "url(https://source.unsplash.com/8WFnEehJWso)",
backgroundRepeat: "no-repeat",
backgroundColor: theme.palette.primary.light,
backgroundSize: "cover",
backgroundPosition: "center"
},
paper: {
height: "30vh",
margin: theme.spacing(1.5, 3),
display: "flex",
flexDirection: "column",
alignItems: "center"
},
mui_form: {
width: "100%",
marginTop: theme.spacing(1)
},
submit: {
margin: theme.spacing(3, 0, 2)
},
card: {
maxWidth: 600
},
media: {
height: 500
},
circle: {
display: "flex",
"& > * + *": {
marginLeft: theme.spacing(2)
},
align: "center"
},
menuButton: {
marginRight: theme.spacing(2)
},
title: {
flexGrow: 1,
alignContent: "center"
},
pie: {
height: 250,
marginBottom: theme.spacing(2)
},
avatar: {
margin: theme.spacing(1),
backgroundColor: theme.palette.secondary.main
},
muiform: {
width: "100%", // Fix IE 11 issue.
marginTop: theme.spacing(1),
"&:focus": {
borderColor: "primary.dark"
}
},
muisubmit: {
margin: theme.spacing(3, 0, 2),
padding: theme.spacing(3, 2),
background: "primary.main",
"&:hover": {
backgroundColor: "primary.dark"
}
},
login: {
padding: "0 30px",
height: 200,
display: "flex",
flexDirection: "column",
justifyContent: "center"
},
loginImage: {
backgroundImage: "url(https://source.unsplash.com/rCbdp8VCYhQ)",
backgroundRepeat: "no-repeat",
backgroundSize: "cover",
backgroundPosition: "center",
height: "100vh"
},
loginSurface: {
height: "40vh",
padding: "20px 10px",
display: "flex",
flexDirection: "column",
alignItems: "center",
background: "black"
},
table: {
minWidth: 650
}
});

export default withStyles(useStyles);

然后我尝试在组件上实现此功能:

import DialogContent from "@material-ui/core/DialogContent";
import DialogContentText from "@material-ui/core/DialogContentText";
import DialogTitle from "@material-ui/core/DialogTitle";
import Slide from "@material-ui/core/Slide";
import Table from "@material-ui/core/Table";
import TableBody from "@material-ui/core/TableBody";
import TableCell from "@material-ui/core/TableCell";
import TableContainer from "@material-ui/core/TableContainer";
import TableHead from "@material-ui/core/TableHead";
import TableRow from "@material-ui/core/TableRow";
import Paper from "@material-ui/core/Paper";
import useStyles from "./useStyles";
import { TransitionProps } from "@material-ui/core/transitions";
import { useTheme } from '@material-ui/core/styles';
import { Theme } from '@material-ui/core';

const Transition = React.forwardRef<unknown, TransitionProps>(
function Transition(props, ref) {
return <Slide direction="up" ref={ref} {...props} />;
}
);

export default function GuidanceDialogue(){
const theme = useTheme<Theme>();
const [open, setOpen] = React.useState(false);
const classes = useStyles(theme);

使用 useTheme 访问主题,并使用根 App.js 中的 ThemeProvider 向下传递。

我收到的错误是:

const classes = useStyles(theme);

“Theme”类型的参数不可分配给“ComponentType”类型的参数; }>>'。 类型“Theme”不可分配给类型“FunctionComponent”; }>>'。 类型“主题”不提供与签名“(props: PropsWithChildren; }>>, context?: any): ReactElement<...> | 匹配的内容null'.ts(2345)

如果我将类型转换为如下:

const classes = useStyles<any>(theme);

所以我可以看看类型错误是否确实是问题所在,或者是否存在其他问题,然后它提到了我所指的 className 属性,例如classes.table,表不存在。

我对整件事感到非常困惑,请帮忙!

最佳答案

解决方案是编写以下样式文件:

 const useStyles = makeStyles((theme: Theme) => createStyles({
root: {

关于javascript - 如何使用 Typescript 实现 Material-UI ThemeProvider 和 WithStyles,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60653969/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com