在 sx prop MUI 5 中传播主题

Ija*_*yam 7 javascript reactjs material-ui

之前使用 v4 的代码是

const useStyles = makeStyles(theme => ({
  toolbarMargin: {
    ...theme.mixins.toolbar
  }
}))
Run Code Online (Sandbox Code Playgroud)

如何使用sxprop 将此代码迁移到 MUI v5,我尝试像这样使用它

<Box
  sx={{
    ...(theme) => theme.mixins.toolbar,
  }}
/>
Run Code Online (Sandbox Code Playgroud)

theme没有定义。

v1s*_*n_4 5

您可以执行以下操作:

<Box sx={theme => theme.mixins.toolbar} />
Run Code Online (Sandbox Code Playgroud)

或者

<Box sx={theme => ({
  ...theme.mixins.toolbar,
  // other sx stuff
})} />
Run Code Online (Sandbox Code Playgroud)

theme由于您还可以将带有参数的函数传递给sxprop,因此它必须返回有效的 sx 对象。

  • 如果我像 `sx` `minHeight: (theme) =&gt; theme.mixins.toolbar.minHeight` 那样定义每个样式,我就能得到结果,如果我使用你的解决方案,我就得到了名为 `css 的 css 类-0` (2认同)