【问题标题】:TypeError: Cannot read property 'root' of undefinedTypeError:无法读取未定义的属性“根”
【发布时间】:2017-12-05 22:34:24
【问题描述】:

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

 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;

现在我有一些错误如下。

 <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/)的一个例子。
然后我将代码放到我的代码中,不做任何更改。

【问题讨论】:

    标签: reactjs material-ui


    【解决方案1】:
    • 你的道具不包含classes
    • 如果您想访问styles 常量中的类,那么您可能需要这样做styles(/*your theme name/*).root

    const { classes } = props; 这样做是分配 classes=undefined 然后当您尝试访问 classes.root 时它会抛出错误。

    现在,你可以改变这个

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

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

    或在 Charity 组件中更改此行

    {&lt;BasicTable /&gt;}

    {&lt;BasicTable classes={styles("")}/&gt;} 给定样式

    【讨论】:

    • 谢谢,它有效。我改为 {}。但是发生了下一个错误。它是“无法读取未定义的属性'单元'”。 (第 19 行:marginTop:theme.spacing.unit * 3,)和第 97 行:{}。你有解决这个错误的想法吗?
    • 你没有提供任何主题对象theme.spacing.unit * 3。现在你可以试试styles({spacing: {unit: 1}}) 而不是styles("")
    • 成功了。谢谢!我会努力学习语法:)
    • 第 21 行:'styles' 未定义 no-undef。
    猜你喜欢
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    • 2019-08-19
    • 1970-01-01
    相关资源
    最近更新 更多