【问题标题】:Cannot get props from useStyles material-ui无法从 useStyles material-ui 获取道具
【发布时间】:2020-07-12 00:43:04
【问题描述】:

我正在尝试在 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 中使用我当前的索引而不是使用内联样式?

【问题讨论】:

  • 为此的许多代码,我只是需要根据索引显示选项卡,我收到了答案,但感谢您的建议:)

标签: reactjs material-ui react-hooks


【解决方案1】:

像这样使用它:

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

or 

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

【讨论】:

  • 不客气,很高兴它有帮助。请接受答案,以便其他访问者可以快速发现它
猜你喜欢
  • 2020-05-08
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 2020-04-01
  • 2020-10-13
  • 2021-11-18
  • 2020-04-14
相关资源
最近更新 更多