【发布时间】:2021-07-19 22:17:19
【问题描述】:
现在我想创建一个组件,并将颜色和值从其父组件传递给组件。在这里,更具体地说,我想将颜色传递给样式,以便我可以根据父组件更改颜色。但我收到错误:错误 TS2339:类型“{}”上不存在属性“颜色”。
import React,{FC} from "react";
import {createStyles, makeStyles} from "@material-ui/core/styles";
import {BackstageTheme} from "@backstage/theme";
import {LinearProgress} from "@material-ui/core";
interface OverallHealthProps {
color?: String;
value?: number;
}
const useStyles = makeStyles<BackstageTheme>(() =>
createStyles({
overAllHealthRoot: {
height: 30,
borderRadius: 0
},
overAllHealthColorPrimary: {
//TODO: change color according to theme
backgroundColor: "#A9A9A9"
},
overAllHealthBar: {
borderRadius: 0
},
overAllHealthBarColorPrimary: {
//Error TS2339: Property 'color' does not exist on type '{}'.
backgroundColor: props => props.color,
}
})
)
const RatingBar: FC<OverallHealthProps> = (props:OverallHealthProps) =>{
const classes = useStyles(props);
return(
<LinearProgress
variant="determinate"
value={props.value}
classes={{
root: classes.overAllHealthRoot,
colorPrimary: classes.overAllHealthColorPrimary,
bar: classes.overAllHealthBar,
barColorPrimary: classes.overAllHealthBarColorPrimary
}}
/>
)
};
export default RatingBar;
我也试过这个:
const useStyles = makeStyles<BackstageTheme, OverallHealthProps>((BackstageTheme, props:OverallHealthProps)=>
createStyles({
overAllHealthRoot: {
height: 30,
borderRadius: 0
},
overAllHealthColorPrimary: {
//TODO: change color according to theme
backgroundColor: "#A9A9A9"
},
overAllHealthBar: {
borderRadius: 0
},
overAllHealthBarColorPrimary: {
//Error TS2339: Property 'color' does not exist on type '{}'.
backgroundColor: props.color,
}
})
)
并得到错误:
TS2345:“(主题:BackstageTheme,道具:OverallHealthProps)=> any”类型的参数不可分配给“样式
那么如何解决这个问题?
【问题讨论】:
标签: reactjs material-ui