arccycle
arccycle

Reputation: 83

TypeError: Cannot read property 'root' of undefined

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 | 

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.

Upvotes: 2

Views: 12022

Answers (1)

Nandu Kalidindi
Nandu Kalidindi

Reputation: 6280

  • 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; doing this is assigning classes=undefined then when you are trying to access classes.root it it throwing the error.

Now, you can either change this

<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("")}/>} given styles

Upvotes: 4

Related Questions