TypeError:无法读取未定义的属性"root" [英] TypeError: Cannot read property 'root' of undefined

查看:139
本文介绍了TypeError:无法读取未定义的属性"root"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Material-Ui中的BasicTable函数放到我的React.js文件中.
这是我的代码.

I'm trying to put a BasicTable function from Material-Ui to my React.js file.
Here is my code.

 import React, { Component } from 'react';
 import { Route, Redirect, Switch, Link, HashRouter} from 'react-router-dom';
 import firebase, { auth, provider } from '../firebase.js';
 import '../App.css';

 // Material-ui theme
 import NavBar from '../profile_pages/navBar';

 //For Table Material-ui
 import classNames from 'classnames';
 import PropTypes from 'prop-types';
 import { withStyles } from 'material-ui/styles';
 import Table, { TableBody, TableCell, TableHead, TableRow } from 'material-ui/Table';
 import Paper from 'material-ui/Paper';

 const styles = theme => ({
  root: {
    width: '100%',
    marginTop: theme.spacing.unit * 3,
    overflowX: 'auto',
  },
  table: {
    minWidth: 700,
  },
});

let id = 0;
function createData(name, calories, fat, carbs, protein) {
  id += 1;
  return { id, name, calories, fat, carbs, protein };
}

const data = [
  createData('Frozen yoghurt', 159, 6.0, 24, 4.0),
  createData('Ice cream sandwich', 237, 9.0, 37, 4.3),
  createData('Eclair', 262, 16.0, 24, 6.0),
  createData('Cupcake', 305, 3.7, 67, 4.3),
  createData('Gingerbread', 356, 16.0, 49, 3.9),
];

function BasicTable(props) {
  const { classes } = props;

  return (
    <Paper className={classes.root}>
      <Table className={classes.table}>
        <TableHead>
          <TableRow>
            <TableCell>Dessert (100g serving)</TableCell>
            <TableCell numeric>Calories</TableCell>
            <TableCell numeric>Fat (g)</TableCell>
            <TableCell numeric>Carbs (g)</TableCell>
            <TableCell numeric>Protein (g)</TableCell>
          </TableRow>
        </TableHead>
        <TableBody>
          {data.map(n => {
            return (
              <TableRow key={n.id}>
                <TableCell>{n.name}</TableCell>
                <TableCell numeric>{n.calories}</TableCell>
                <TableCell numeric>{n.fat}</TableCell>
                <TableCell numeric>{n.carbs}</TableCell>
                <TableCell numeric>{n.protein}</TableCell>
              </TableRow>
            );
          })}
        </TableBody>
      </Table>
    </Paper>
  );
}

class Charity extends Component {
  constructor() {
    super()
    this.state = {
      open: false
    }
  }

  //For nav
  handleToggle() {
    this.setState({
      open: !this.state.open
    })
  }

  render() {
   return (
      <div>
        <NavBar
          onToggle={() => this.handleToggle()}
          open={this.state.open}
        />

        {<BasicTable/>}
      </div>
    );
  }
}

BasicTable.propTypes = {
  classes: PropTypes.object.isRequired,
};

export default Charity;

现在我有一些错误,如下所示.

Now I have some errors as following.

 <BasicTable>

  42 |  const { classes } = props;
  43 | 
  44 |  return (
> 45 |    <Paper className={classes.root}>
  46 |      <Table className={classes.table}>
  47 |        <TableHead>
  48 |          <TableRow>

<./src/index.js>
   8 | //make click events enable to use 
   9 | injectTapEventPlugin();
  10 | 
> 11 | ReactDOM.render(<Root/>, document.getElementById('root'));
  12 | registerServiceWorker();
  13 | 
  14 | 

我在操作上要叠加的内容是关于在Charity类的呈现方式上调用BasicTable(props)函数的方式.

我应该如何解决该错误?
BasicTable()函数来自Material-ui网站的示例( https://material -ui-next.com/demos/tables/).
然后,我将代码直接放在我的代码上.

What I'm stacking on the action is about the way to call the BasicTable(props) function at the render of Charity class.

How should I resolved the error?
The BasicTable() function is brought from an example of Material-ui website (https://material-ui-next.com/demos/tables/).
Then I put the code to my one without any changes.

推荐答案

  • 您的道具不包含classes
  • 如果要访问styles常量中的类,则可能需要执行styles(/*your theme name/*).root
    • You props does not contain classes
    • If you want to access classes that are in the styles constant, then you might want to do this styles(/*your theme name/*).root
    • const { classes } = props;这样做是在分配classes=undefined,然后在尝试访问classes.root时会抛出错误.

      const { classes } = props; doing this is assigning classes=undefined then when you are trying to access classes.root it it throwing the error.

      现在,您可以更改此

      <Paper className={classes.root}>
            <Table className={classes.table}>
      

      TO

      // Because the theme argument is never used in the function
      <Paper className={styles("").root}>
            <Table className={styles("").table}>
      

      或在慈善"组件中更改此行

      OR change this line in Charity component

      {<BasicTable />}

      TO

      {<BasicTable classes={styles("")}/>}给定样式

      这篇关于TypeError:无法读取未定义的属性"root"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆