使主题在HOC组件中可用

时间:2019-06-24 19:59:38

标签: reactjs material-ui

我正在尝试使用Material UI集合中的导航栏,但是该组件是作为函数编写的,并且正在使用钩子。我试图将组件转换为HOC(类),但是在访问组件中的主题时遇到问题。组件中的主题未定义

const styles = theme => ({
  root: {
    display: "flex"
  },
});

<IconButton onClick={this.handleDrawerClose}>
      {theme.direction === "ltr" ? (
         <ChevronLeftIcon />
      ) : (
         <ChevronRightIcon />
      )}
</IconButton>

1 个答案:

答案 0 :(得分:1)

尝试一下:

import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core';
import Paper from './Paper';

const styles = () => ({
  root: {
    display: 'flex'
  }
});

const Bubble = props => {
  const { classes } = props;
  return (
   <IconButton className={classes.root} onClick={this.handleDrawerClose}></IconButton>
  );
};

export default withStyles(styles)(Bubble);