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
357 views
in Technique[技术] by (71.8m points)

reactjs - How to use 'theme' and 'props' in makeStyles?

How do I write makeStyles() so that it allows me to use both theme variables and props?

I've tried this:

const useStyles = makeStyles(theme => ({
  appbar: props => ({
    boxShadow: "none",
    background: "transparent",
    marginTop: theme.spacing(2),
    marginBottom: theme.spacing(2),
    color: theme.palette.getContrastText(props)
  }),
}));

And called it in the component with:

const classes = useStyles(backgroundColor);

Where backgroundColor is a prop on the component with type CSSProperties["backgroundColor"]

But I'm getting the error:

TypeError: Cannot read property 'rules' of undefined
    at RuleList.onUpdate (C:Users...
ode_modulesjssdistjss.cjs.js:944:14)
    at RuleList.update (C:Users...
ode_modulesjssdistjss.cjs.js:923:12)
    at StyleSheet.update (C:Users...
ode_modulesjssdistjss.cjs.js:1178:39)
    at attach (C:Users...
[email protected]:141:18)
    at C:Users...
[email protected]:262:7
    at useSynchronousEffect (C:Users...
[email protected]:207:14)
    at C:Users...
[email protected]:254:5
    at Layout (C:Users....nextserverstaticdevelopmentpagesindex.js:1698:17)
    at processChild (C:Users...
ode_modules
eact-domcjs
eact-dom-server.node.development.js:2888:14)
    at resolve (C:Users...
ode_modules
eact-domcjs
eact-dom-server.node.development.js:2812:5)
    at ReactDOMServerRenderer.render (C:Users...
ode_modules
eact-domcjs
eact-dom-server.node.development.js:3202:22)
    at ReactDOMServerRenderer.read (C:Users...
ode_modules
eact-domcjs
eact-dom-server.node.development.js:3161:29)
    at renderToString (C:Users...
ode_modules
eact-domcjs
eact-dom-server.node.development.js:3646:27)
    at render (C:Users...
ode_modules
ext-serverdistserver
ender.js:86:16)
    at renderPage (C:Users...
ode_modules
ext-serverdistserver
ender.js:211:20)
    at ctx.renderPage (C:Users....nextserverstaticdevelopmentpages\_document.js:2404:22)

  100 |   handleSignUpClick,
  101 |   backgroundColor
  102 | }) => {
> 103 |   const classes = useStyles(backgroundColor);
  104 |   return (
  105 |     <AppBar className={classes.appbar}>
  106 |       <Container maxWidth="lg">

edit: I'm using version 4.0.0-beta.1 of material core and styles

question from:https://stackoverflow.com/questions/56111294/how-to-use-theme-and-props-in-makestyles

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

1 Reply

0 votes
by (71.8m points)

Tested with:

"@material-ui/core": "^4.0.0-beta.1",
"@material-ui/styles": "^4.0.0-rc.0",

JavaScript example:

my-component.js

import React from 'react';
import { makeStyles } from '@material-ui/styles';

import { useStyles } from './my-component.styles.js'; 

const myComponent = (props) => {
    const styleProps = { width: '100px', height: '100px' };
    const classes = useStyles(styleProps);

    return (
        <div className={classes.mySelector}></div> // with 100px and height 100px will be applied
    )
}

my-component.styles.js

export const useStyles = makeStyles(theme => ({
    mySelector: props => ({
        display: 'block',
        width: props.width,
        height: props.height,
    }),
}));

TypeScript example:

my-component.ts

import React, { FC } from 'react'; 
import { makeStyles } from '@material-ui/styles';

import { useStyles } from './my-component.styles.ts'; 
import { MyComponentProps, StylesProps } from './my-component.interfaces.ts'; 

const myComponent: FC<MyComponentProps> = (props) => {
    const styleProps: StylesProps = { width: '100px', height: '100px' };
    const classes = useStyles(styleProps);

    return (
        <div className={classes.mySelector}></div> // with 100px and height 100px will be applied
    )
}

my-component.interfaces.ts

export interface StyleProps {
    width: string;
    height: string;
}

export interface MyComponentProps {
}

my-component.styles.ts

import { Theme } from '@material-ui/core';
import { makeStyles } from '@material-ui/styles';

import { StyleProps } from './my-components.interfaces.ts';

export const useStyles = makeStyles<Theme, StyleProps>((theme: Theme) => ({
    mySelector: props => ({ // props = { width: string; height: string }
        display: 'block',
        width: props.width,
        height: props.height,
    }),
}));

Update

Re-tested with

"@material-ui/core": "^4.12.X"

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

...