Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
130 views
in Technique[技术] by (71.8m points)

javascript - How can I pass my theme classes in my react function to stylize?

Trying to stylize my React header buttons, however I cannot pass my classes function which uses useStyles()

The error originations from: className: {classes.menuButton} in my getMenuButtons function

const useStyles = makeStyles((theme) => ({
    appBar: {
        borderBottom: `1px solid ${theme.palette.divider}`,
    },
    link: {
        margin: theme.spacing(1, 1.5),
    },
    toolbarTitle: {
        flexGrow: 1,
        fontFamily: 'Track',
        textAlign: "left"
    },
    menuButton: {
        fontWeight: 700,
        size: "18px",
        marginLeft: "38px",
    }
}));


function Header() {
    const classes = useStyles();

    const displayDesktop = () => {
        return <Toolbar>
                    {WelcomeLogo }
                    {getMenuButtons()}
                </Toolbar>;
      };

    const WelcomeLogo = (
        <Typography variant="h6" component="h1" className={classes.toolbarTitle} color="inherit">
          Welcome
        </Typography>
      );
    
    const getMenuButtons = () => {
        return headersData.map(({ label, href }) => {
            return (
                <Button
                {...{
                    key: label,
                    color: "inherit",
                    to: href,
                    component: RouterLink,
                    className: {classes.menuButton}
                }}
                >
                    {label}
                </Button>
          );
        });
      };

The only error I'm getting is failed to compile, I want to be able to add my style classes to getMenuButtons() for button styling, thank you for help!

question from:https://stackoverflow.com/questions/65924560/how-can-i-pass-my-theme-classes-in-my-react-function-to-stylize

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The error you're getting is because the className prop is a string prop, not an object prop and you're treating it as an object but you're not setting a key for that object value, that is why it won't compile, because the syntax is wrong, do this instead:

<Button
 {...{
   key: label,
   color: "inherit",
   to: href,
   component: RouterLink,
   className: classes.menuButton
  }
 }
>
 {label}
</Button>

Suggestion: I would use the regular syntax in the Button component because you're not making any conditional prop that might require the use of the object syntax

<Button
 key={label}
 color="inherit"
 to={href}
 component={RouterLink}
 className={classes.menuButton}
>
 {label}
</Button>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...