类型错误:无法读取未定义的属性“root”
2017-12-05
11793
我试图将 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/
)。
然后我将代码放入我的代码中,没有任何更改。
1个回答
-
您的 props 不包含
classes
-
如果您想访问
styles
常量中的类,那么您可能需要执行此操作styles(/*your theme name/*).root
const { classes } = props;
这样做会分配
classes=undefined
,然后当您尝试访问
classes.root
时会抛出错误。
现在,您可以将其更改为
<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}>
或更改 Charity 组件中的此行
{<BasicTable />
TO
{<BasicTable classes={styles("")}/>
给定样式
Nandu Kalidindi
2017-12-05