【问题标题】:Material UI: Cannot read property 'button' of undefined材质 UI:无法读取未定义的属性“按钮”
【发布时间】:2020-05-12 02:42:34
【问题描述】:
import { createMuiTheme, ThemeOptions } from '@material-ui/core/styles';

const theme = (options: ThemeOptions) => {
    return createMuiTheme({
        palette: {
            primary: {
                main: '#b5ddd1'
            },
            secondary: {
                main: '#b2d9ea'
            },
        },
        typography: {
            fontFamily: 'Raleway, Arial',
            button: {
                fontStyle: 'italic',
            },
        },
        ...options,
    })
}

提供者

import { ThemeProvider } from '@material-ui/core/styles'

         <Provider store={store}>
            <ConnectedRouter>
                <ThemeProvider theme={theme}>
                    <GlobalStyles/>
                    {/*<ErrorBoundary>*/}
                    {/*<Layout>*/}
                    <Component {...pageProps} />
                    {/*</Layout>*/}
                    {/*</ErrorBoundary>*/}
                </ThemeProvider>
            </ConnectedRouter>
        </Provider>
import Button from '@material-ui/core/Button'

            <div>
                <div>Log into APP</div>
                <Button>Test</Button>
            </div>

但我仍然在 Button.js 中遇到错误

在样式(/Users/filipbrezina/Documents/Projekty/sportee-frontend/node_modules/ material-ui/core/Button/Button.js:33:78

有人可以帮帮我吗?我不知道我做错了什么????

【问题讨论】:

  • 能否请您添加codeandbox URL,以便我们可以帮助您调试问题。

标签: reactjs material-ui


【解决方案1】:

您定义/提供theme 的方式存在错误:

你需要这样提供:

<ThemeProvider theme={theme({})}> {/* function call: theme({}) */}

或者如果你这样定义:

const theme = createMuiTheme({
  palette: {
    primary: {
      main: '#b5ddd1',
    },
    // you can add more options
  }
})

你可以这样提供:

<ThemeProvider theme={theme}>

【讨论】:

    猜你喜欢
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 2020-04-22
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    相关资源
    最近更新 更多