我正在将我的MUI迁移到v5,并且我遇到了Make Styes"插补"模式的问题,如下所示:

 const useStyles = makeStyles((theme) => ({
  root: {
    backgroundColor: (props) => {
      let bgColor = "inherit"
      if (props.colorPalette.toLowerCase() === COLORS.SAND) {
        bgColor = theme.palette.background.sand
      } else if (props.isGreenBG) {
        bgColor = theme.palette.primary.main
      } else {
        bgColor = theme.palette.common.white
      }
      return bgColor
    },
  },
  heading: {
    color: (props) => {
      if (props.isGreenBG) {
        return theme.palette.common.white
      }
      return theme.palette.primary.main
    },
    marginBottom: `15px`,
    letterSpacing: `normal`,
    [theme.breakpoints.down(`xs`)]: {
      fontSize: `2rem`,
      lineHeight: `2rem`,
    },
  },
  subhead: {
    color: (props) => {
      if (props.isGreenBG) {
        return theme.palette.common.white
      }
      return theme.palette.common.black
    },
    lineHeight: `1.5rem`,
    fontSize: `1.25rem`,
    [theme.breakpoints.down(`md`)]: {
      lineHeight: `1.125rem`,
      fontSize: `1rem`,
    },
  },
  body: {
    color: (props) => {
      if (props.isGreenBG) {
        return theme.palette.common.white
      }
      return theme.palette.text.lightHeader
    },
  },
}))

export const MyComponent = ({ colorPalette }) => {
  const isGreenBG = colorPalette.toLowerCase() === COLORS.GREEN1
  const classes = useStyles({
    isGreenBG,
    colorPalette,
  })

  // ...rest of component, for example...
  return <h1 className={classes.heading}>Just an example</h1>
}

我使用了code-mod,发现这些TODO://TODO JSS-to-TSS-Reaction代码:无法可靠地处理样式定义.Css属性中的ArrowFunctionExpression.

Here is what is displayed for the value in Chrome dev tools: enter image description here

有没有办法通过SSR支持这种类型的模式?它在CSR上的工作与预期一样,除了重写之外,我找不到其他解决方案,但保留这个功能会很好.谢谢!

推荐答案

TSS-Reaction不支持针对单个样式规则或CSSprops 的函数,但makeStyles调用的主体是一个函数,因此所有逻辑都可以移到该函数的顶部.

下面是您的代码的工作版本(尽管我将样式减少到只有rootheading).除了将函数逻辑移到样式定义之上之外,您还需要在makeStyles调用的第一行中的主题之后指定参数(例如(theme, { colorPalette, isGreenBG })).

import { makeStyles } from "tss-react/mui";

const useStyles = makeStyles()((theme, { colorPalette, isGreenBG }) => {
  let bgColor = "inherit";
  if (colorPalette.toLowerCase() === "sand") {
    bgColor = theme.palette.background.sand;
  } else if (isGreenBG) {
    bgColor = theme.palette.primary.main;
  } else {
    bgColor = theme.palette.common.white;
  }
  const color = isGreenBG
    ? theme.palette.common.white
    : theme.palette.primary.main;
  return {
    root: {
      backgroundColor: bgColor
    },
    heading: {
      color,
      marginBottom: `15px`,
      letterSpacing: `normal`,
      [theme.breakpoints.down(`xs`)]: {
        fontSize: `2rem`,
        lineHeight: `2rem`
      }
    }
  };
});

const MyComponent = ({ colorPalette }) => {
  const isGreenBG = colorPalette.toLowerCase() === "green1";
  const { classes } = useStyles({
    isGreenBG,
    colorPalette
  });

  return (
    <div className={classes.root}>
      <h1 className={classes.heading}>Just an example</h1>
    </div>
  );
};
export default function Demo() {
  return <MyComponent colorPalette="green1" />;
}

Edit makeStyles with props

Reactjs相关问答推荐

在带有和设计的表格中禁用和取消选中复选框

props 可以在Reaction中锻造吗?

在Reaction中更新状态时,我将变得未定义

如何将图像(在代码中称为自己)定位在蓝色圈内的点上?我正在使用material UI和Reaction

错误:操作必须是普通对象.使用自定义中间件进行异步操作. Redux/工具包

React redux 工具包 useGetFilterProductsQuery 和 useGetFilterProductsByCategoriesQuery 未定义为函数

Next.js `getLayout` 函数没有被调用

在 useSelector() 中使用 array.map() 如何导致组件在分派操作时重新渲染?

使用jest如何覆盖对象的模拟?我正在使用`jest.mock()`来模拟对象,并希望 for each 测试用例覆盖模拟.

无法设置 null 的属性(设置src)

React - 使用 useEffect 还是直接在 onChange 方法中更改值对性能最好?

如何测试使用 React.createportal 呈现的组件?

Primereact 的日历组件在每个月 12 日之后使用非唯一键生成

将 Material UI 菜单渲染为

是什么导致了这个令人惊讶的数组导致 React

如何在 JSX 中使用 -webkit- 前缀?

为什么我在运行一些 npm react 命令时会收到这些警告?

如何通过 Material Ui 设计将 Accordion 添加到您的 React 项目中?

axios post方法中的请求失败,状态码为500错误

useEffect 仅在跟随链接后有效,不适用于直接链接