我为什么会收到类未定义的错误?

huangapple go评论63阅读模式
英文:

Why am I getting classes undefined error?

问题

I'm new to React, can someone explain to me why I am getting classes undefined error?

我是React新手,可以有人解释一下为什么我会收到未定义的classes错误?

Below is the code

以下是代码

const styles = (theme) => ({
    root: {
      width: "100%",
      marginTop: theme.spacing.unit * 3,
      overflowX: "auto"
    },
    table: {
      minWidth: 700
    },
    tableRow: {
      "&.Mui-selected, &.Mui-selected:hover": {
        backgroundColor: "purple",
        "& > .MuiTableCell-root": {
          color: "yellow"
        }
      }
    }
  });
export default function MyTable(props) {
    const { classes } = props;
    return (
        <div>
            <Box>
                <TableContainer component={Paper}>
                    <Table className={classes.table}>

I'm getting uncaught runtime errors from classes.table because it's undefined. What did I do wrong?

我从classes.table中收到未捕获的运行时错误,因为它是未定义的。我做错了什么?

英文:

I'm new to React, can someone explain to me why I am getting classes undefined error?

Below is the code

const styles = (theme) =&gt; ({
    root: {
      width: &quot;100%&quot;,
      marginTop: theme.spacing.unit * 3,
      overflowX: &quot;auto&quot;
    },
    table: {
      minWidth: 700
    },
    tableRow: {
      &quot;&amp;.Mui-selected, &amp;.Mui-selected:hover&quot;: {
        backgroundColor: &quot;purple&quot;,
        &quot;&amp; &gt; .MuiTableCell-root&quot;: {
          color: &quot;yellow&quot;
        }
      }
    }
  });
export default function MyTable(props) {
    const { classes } = props;
    return (
        &lt;div&gt;
            &lt;Box&gt;
                &lt;TableContainer component={Paper}&gt;
                    &lt;Table className={classes.table}&gt;

I'm getting uncaught runtime errors from classes.table because it's undefined. What did I do wrong?

答案1

得分: 1

// 请注意,在注释中提到,您没有将`classes`属性传递给`MyTable`组件。这就是为什么您无法从`props`解构`classes`并得到`undefined`的原因。

// 您可以使用`@material-ui/core/styles`中的`makeStyles`和`createStyles`函数。

import { makeStyles, createStyles } from "@material-ui/core/styles";

const useStyles = makeStyles((theme: Theme) =>
  createStyles({
    root: {
      width: "100%",
      marginTop: theme.spacing.unit * 3,
      overflowX: "auto",
    },
    table: {
      minWidth: 700,
    },
    tableRow: {
      "&.Mui-selected, &.Mui-selected:hover": {
        backgroundColor: "purple",
        "& > .MuiTableCell-root": {
          color: "yellow",
        },
      },
    },
  })
);

// 现在您可以在组件中使用`useStyles`钩子来获取这些类。

export default function MyTable(props) {
  const classes = useStyles();
  return (
    <div>
      <Box>
        <TableContainer component={Paper}>
          <Table className={classes.table}>
            ...
          </Table>
        </TableContainer>
      </Box>
    </div>
  );
}

查看MUI v4 styles basics获取更多信息。

英文:

As mentioned in the comments you're not passing a classes property to the MyTable component. This is why you can't destructure classes from props and get undefined.

You can use the makeStyles and createStyles functions from @material-ui/core/styles.

import { makeStyles, createStyles } from &quot;@material-ui/core/styles&quot;;

const useStyles = makeStyles((theme: Theme) =&gt;
  createStyles({
    root: {
      width: &quot;100%&quot;,
      marginTop: theme.spacing.unit * 3,
      overflowX: &quot;auto&quot;,
    },
    table: {
      minWidth: 700,
    },
    tableRow: {
      &quot;&amp;.Mui-selected, &amp;.Mui-selected:hover&quot;: {
        backgroundColor: &quot;purple&quot;,
        &quot;&amp; &gt; .MuiTableCell-root&quot;: {
          color: &quot;yellow&quot;,
        },
      },
    },
  })
);

Now you can use the useStyles hook in your component to get the classes.

export default function MyTable(props) {
  const classes = useStyles();
  return (
    &lt;div&gt;
      &lt;Box&gt;
        &lt;TableContainer component={Paper}&gt;
          &lt;Table className={classes.table}&gt;
            ...
          &lt;/Table&gt;
        &lt;/TableContainer&gt;
      &lt;/Box&gt;
    &lt;/div&gt;
  );
}

See MUI v4 styles basics for more information.

huangapple
  • 本文由 发表于 2023年5月17日 23:36:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定