【问题标题】:How to pass props to createStyles in React如何在 React 中将 props 传递给 createStyles
【发布时间】: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”类型的参数不可分配给“样式”类型的参数。类型“(主题:BackstageTheme,道具:OverallHealthProps)=> any”不可分配给类型“StyleRulesCallback”。

那么如何解决这个问题?

【问题讨论】:

    标签: reactjs material-ui


    【解决方案1】:

    只需将默认颜色道具值传递给您的箭头函数。问题在于初始化,因为非颜色传递给您的函数。试试这样:makeStyles&lt;BackstageTheme&gt;(({color = "&lt;something&gt;" }) =&gt; ... 在你传递了 props 之后,新的值就会发生。

    【讨论】:

    • 谢谢你的回答,但我需要根据道具改变我的颜色。颜色应该是变量吧?
    • 问题是初始化,在你传递props之后,新的值就会发生。另一种方法是检查道具是否存在,为此您可以尝试使用try-catch block @HongliBu
    • 直接回答您的问题,您使用的color 应该是箭头函数的输入变量(道具对象中的键)
    • 我试过了,它似乎不起作用,当我通过{color = "&lt;something&gt;" } 时,我得到“TS2339:属性'颜色'不存在于类型'BackstageTheme”上,我认为它考虑@ 987654327@作为后台主题
    • 您可以将您的项目添加到sandbox 或stackblitz 吗?会更容易帮到你
    猜你喜欢
    • 1970-01-01
    • 2019-08-11
    • 2021-05-06
    • 2019-04-06
    • 1970-01-01
    • 2020-04-10
    • 2023-03-08
    • 2020-04-02
    • 2021-03-17
    相关资源
    最近更新 更多