开发者问题收集

Material UI:无法读取未定义的属性“按钮”

2020-05-07
2787
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,
    })
}

provider

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 中收到错误

at style (/Users/filipbrezina/Documents/Projekty/sportee-frontend/node_modules/ material-ui/core/Button/Button.js:33:78

有人能帮帮我吗?我不知道我做错了什么 😏

1个回答

您在定义/提供 主题 的方式上存在错误:

您需要像这样提供它:

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

或者如果您像这样定义它:

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

您可以通过这种方式提供它:

<ThemeProvider theme={theme}>
Ajeet Shah
2020-05-11