如何访问 ThemeProvider 子项中的 Material-UI 自定义主题

How to access Material-UI custom theme inside children of ThemeProvider

我想访问我在 CustomCheckbox.js 组件内的自定义 MUI theme 中设置的颜色,但找不到如何操作。

import { createTheme, ThemeProvider } from "@material-ui/core/styles";
import CustomCheckbox from "./CustomCheckbox";

export default function someFunction(){
  const theme = createTheme({
    palette: {
      type: "dark",
      background: {
        default: "#242729",
        paper: "#323638",
    },

  return (
  <ThemeProvider theme={theme}>
    <CustomCheckbox defaultChecked />
  </ThemeProvider>
  )
}

我搜索了很多,但没有找到任何东西。有什么想法吗?

您可以使用

const useStyles = makeStyles((theme) => ({
  paper: {
    backgroundColor: theme.palette.background.paper,
  }, 
}))

在 CustomCheckbox 组件中:

const CustomCheckbox = () => {
  const classes = useStyles()
  ....
  return (
     <div className={classes.paper}>
  ....