首页 > 解决方案 > 无法从 useStyles material-ui 获取道具

问题描述

我正在尝试在 useStyles 挂钩中传递我当前的索引,以根据这个索引显示我的组件,如下所示:

const useStyles = makeStyles(theme => {
  console.log(theme);
  return {
    root: {
      flexGrow: 1,
      display: theme.props.currentIndex !== 0 ? 'none' : void 0
    }
  };
});

我看到我们可以将 props 传递给 useStyle 钩子,所以我传递了我当前的值以在 makeStyles 中使用它:

const AcademicDegresPanel = props => {
  const classes = useStyles(props);
  console.log(props.currentIndex)
  return (
    <Grid container className={classes.root}>
      Degrés academique
    </Grid>
  );
};

但是当我记录我的主题时,它在 props 键中没有 currentIndex :

palette: {common: {…}, type: "light", primary: {…}, secondary: {…}, error: {…}, …}
props:
__proto__: Object

如何在 makeStyles 中使用我当前的索引而不是使用内联样式?

标签: reactjsmaterial-uireact-hooks

解决方案


像这样使用它:

const useStyles = props => makeStyles(theme => ({/* ... */ })();

or 

const useStyles = props => {
// style construction
return makeStyles(style)();
};

推荐阅读