0

我正在将我的小项目重写为 material-ui,并且我正在使用这个 material-ui-next reactjs 库;对于一个组件 withStyle 装饰器工作得很好,对于另一个它没有用样式装饰组件并引发此错误:

Uncaught TypeError: Cannot read property 'root' of undefined at Respondent.render (MuiRespondent.tsx:48) at vendor.js?v=OjVxDpV6p_Jfz2P38F_R2lc3pjVsUisUejeIABZq7AE:30228 at measureLifeCyclePerf (vendor.js?v=OjVxDpV6p_Jfz2P38F_R2lc3pjVsUisUejeIABZq7AE:29508) at ReactCompositeComponentWrapper._renderValidatedComponentWithoutOwnerOrContext ( vendor.js?v=OjVxDpV6p_Jfz2P38F_R2lc3pjVsUisUejeIABZq7AE:30227) at ReactCompositeComponentWrapper._renderValidatedComponent (vendor.js?v=OjVxDpV6p_Jfz2P38F_R2lc3pjVsUisUejeIABZq7AE:30254) at ReactCompositeComponentWrapper.performInitialMount (vendor.js?v=OjVxDpV6p_Jfz2P38F_R2lc3pjVsUisUejeIABZq7AE:29794) at ReactCompositeComponentWrapper.mountComponent (vendor.js?v =OjVxDpV6p_Jfz2P38F_R2lc3pjVsUisUejeIABZq7AE:29690)在 Object.mountComponent(供应商。js?v=OjVxDpV6p_Jfz2P38F_R2lc3pjVsUisUejeIABZq7AE:12868) at ReactCompositeComponentWrapper._updateRenderedComponent (vendor.js?v=OjVxDpV6p_Jfz2P38F_R2lc3pjVsUisUejeIABZq7AE:30197) at ReactCompositeComponentWrapper._performComponentUpdate (vendor.js?v=OjVxDpV6p_Jfz2P38F_R2lc3pjVsUisUejeIABZq7AE:30156)

引发错误的组件:

import * as React from 'react';
import Button from 'material-ui/Button';
import Dialog, {
  DialogTitle,
  DialogContent,
  DialogContentText,
  DialogActions,
} from 'material-ui/Dialog';
import Typography from 'material-ui/Typography';
import withStyles, { WithStyles, StyleRulesCallback } from 'material-ui/styles/withStyles';
import { RouteComponentProps } from 'react-router';
import withRoot from '../../withRoot';

const styles: StyleRulesCallback<'root'> = theme => ({
  root: {
    textAlign: 'center',
    paddingTop: theme.spacing.unit * 20,
  },
});

type State = {
  open: boolean;
};

export class Respondent extends React.Component<WithStyles<'root'>, State> {
    constructor(props: any){
        super(props);

        this.state = {
            open: false,
          }
    };

  handleClose = () => {
    this.setState({
      open: false,
    });
  };

  handleClick = () => {
    this.setState({
      open: true,
    });
  };

  render() {
    return (
      <div className={this.props.classes.root}>
        <Dialog open={this.state.open} onClose={this.handleClose}>
          <DialogTitle>Super Secret Password</DialogTitle>
          <DialogContent>
            <DialogContentText>1-2-3-4-5</DialogContentText>
          </DialogContent>
          <DialogActions>
            <Button color="primary" onClick={this.handleClose}>
              OK
            </Button>
          </DialogActions>
        </Dialog>
        <Typography variant="display1" gutterBottom>
          Material-UI
        </Typography>
        <Typography variant="subheading" gutterBottom>
          example project
        </Typography>
        <Button variant="raised" color="secondary" onClick={this.handleClick}>
          Super Secret Password
        </Button>
      </div>
    );
  }
}

export default withRoot(withStyles(styles)<{}>(Respondent));

这是我的 withRoot 装饰器:

import * as React from 'react';
import { MuiThemeProvider, createMuiTheme } from 'material-ui/styles';
import * as colors from 'material-ui/colors';
import CssBaseline from 'material-ui/CssBaseline';

// A theme with custom primary and secondary color.
// It's optional.
const theme = createMuiTheme({
  palette: {
    primary: colors.lightBlue,
    secondary: colors.blueGrey,
  },
});

function withRoot(Component: React.ComponentType) {
  function WithRoot(props: object) {
    // MuiThemeProvider makes the theme available down the React tree
    // thanks to React context.
    return (
      <MuiThemeProvider theme={theme}>
        <div>
            {/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
            <CssBaseline />
            <Component {...props} />
        </div>
      </MuiThemeProvider>
    );
  }

  return WithRoot;
}

export default withRoot;

withStyle 装饰器适用的另一个组件看起来几乎相同,我想我遗漏了一些明显的东西,但看不到什么

4

0 回答 0