React&Material-UI:使用TypeScript在createStyles()中未定义主题

时间:2018-12-25 21:46:50

标签: reactjs typescript material-design

我正在学习使用TypeScript进行React,并将Material UI框架用于前端。我尝试使媒体查询正常工作,但出现错误:

  

未捕获的TypeError:无法读取未定义的属性“ up”       样式(webpack-internal:///..app/components/navigation/Toolbar/index.tsx:59)

这是相应的代码:

const styles = ({breakpoints}: Theme) => createStyles( {
    grow: {
        flexGrow: 1
    },
    menuButton: {
        marginLeft: -12,
        marginRight: 20
    },
    sectionDesktop: {
        display: 'none',
        [breakpoints.up('md')]: {
            display: 'flex'
        }
    },
    sectionMobile: {
        display: 'flex'
    },
})

样式通过以下方式传递到组件:

export default withStyles(styles)(Toolbar)

我读到不需要创建自定义主题,因为默认主题将自动传递给函数。但是,主题的breakpoints属性未定义,导致页面空白。

感谢您的帮助

修改

这是该组件的代码,如果没有其他任何组件,仍然会产生问题。

import * as React from 'react'
import {
    Theme
} from '@material-ui/core'
import {
    createStyles,
    WithStyles,
    withStyles
} from '@material-ui/styles'
// import Drawer from '../Drawer'

const styles = ({breakpoints}: Theme) => createStyles( {
    grow: {
        flexGrow: 1
    },
    menuButton: {
        marginLeft: -12,
        marginRight: 20
    },
    sectionDesktop: {
        display: 'none',
        [breakpoints.up('md')]: {
            display: 'flex'
        }
    },
    sectionMobile: {
        display: 'flex'
    },
})

namespace Toolbar {
    interface Props {
    }

    export interface State {
        isOpen : boolean
        mobileMoreAnchorEl? : EventTarget & HTMLElement
    }

    export type AllProps = Props & WithStyles<typeof styles>
}

class Toolbar extends React.Component<Toolbar.AllProps, Toolbar.State> {
    constructor(props: Toolbar.AllProps, context?: any) {
        super(props, context);
        this.state = { isOpen: false, mobileMoreAnchorEl: undefined}
    }

    render() {
        const { classes } = this.props
        // const { isOpen } = this.state

        return(
            <React.Fragment>
                <div className={classes.sectionDesktop}>
                    Hello
                </div>
                <div className={classes.sectionMobile}>
                    World
                </div>
            </React.Fragment>
        )
    }

}

export default withStyles(styles)(Toolbar)

main.tsx(也称为index.js)如下所示:

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createBrowserHistory } from 'history';
import { configureStore } from 'app/store';
import { Router } from 'react-router';
import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
import { App } from './app';

// prepare store
const history = createBrowserHistory()
const store = configureStore()
const theme = createMuiTheme()

ReactDOM.render(
  <Provider store={store}>
    <Router history={history}>
      <MuiThemeProvider theme={theme} >
        <App />
      </MuiThemeProvider>
    </Router>
  </Provider>,
  document.getElementById('root')
);

因此,添加MuiThemeProvider没有帮助。

1 个答案:

答案 0 :(得分:2)

您可以读到here @material-ui/styles不稳定(alpha版本)。

您会在我的CodeSandbox中注意到我正在使用:

import { withStyles, createStyles } from "@material-ui/core/styles";

而不是从@material-ui/styles导入它们。当我使用与您相同的导入文件时,便可以重现您的问题。

Edit 82kp602xqj