如何在类组件中使用makeStyles
API? 该文档仅显示功能组件的示例。
当我尝试在类组件的render
方法中使用此方法时,出现以下错误:
Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://fb.me/react-invalid-hook-call for tips about how to debug and fix this problem.
您不能在类组件内部使用钩子。 您需要功能组件或使用更高的顺序( withStyles
)。
我要结束了,如果我们看到类似的报告,我认为我们应该记录下来。
@oliviertassinari :但是如果将app声明为类,该怎么办?
因为在应用程序中,我具有状态和其他功能。
@ Angelk90您应该使用高阶组件API: https : withStyles
)。
@oliviertassinari
如何在HOC中使用makeStyles?
根据我的用例,我需要样式的材质主题对象。
如@ ghosh-jaideep所建议,
如何使用主题实用程序,例如:
[theme.breakpoints.up("md")]: {
paddingLeft: theme.spacing(2),
paddingTop: theme.spacing(2)
}
在类组件内部?
谢谢
我用withStyles
代替makeStyle
这样的...
import React from 'react';
import { withStyles } from '@material-ui/core/styles';
const styles = (theme) => ({
fab: {
position: 'fixed',
bottom: theme.spacing(2),
right: theme.spacing(2),
},
});
class LoginComponent extends React?Component {
render() {
const { classes } = this.props;
/** your UI components... */
}
}
export default withStyles(syles)(LoginComponent);
@ Ess-Soft如何从父组件渲染LoginComponent
?
ThemeProvider
?如果您共享父组件的代码段,它将更加有用。
我做了与@ Ess-Soft相同的操作,并在withStyles之前添加了widthTheme
withTheme(withStyles(useStyles)(LoginComponent ))
那么即使在render
内部也可以使用theme
render
有人可以解释带样式的东西。 我可以诚实地说,当我看到类似这样的内容时,我认为大声笑出来的人是Angular,这是如此的正常。 今天3个小时搜索并寻找答案,然后我找到了。
@ xtianus79此API也可用于类组件: https : //material-ui.com/styles/basics/#higher -order -component-api。
@oliviertassinari您可以使用网格显示它的示例吗? 大声笑,这就是我想要做的,我被困在那里。 我正在使用makeStyles和createStyles,我无法弄清楚没有功能怎么办? 你能帮忙吗
@ xtianus79您将在StackOverflow上找到更多帮助
对于该特定问题和用例,我没有看到@oliviertassinari 。
嗯甚至https://material-ui.com/styles/basics/#higher -order-component-api都没有回答如何使用“主题”? 为什么仅使用Hooks支持功能组件? 人们确实有遗留代码或需要基于类的组件。
@DewangS请参阅withTheme()
API: https : //material-ui.com/styles/api/#withtheme -component -component。
最有用的评论
我用
withStyles
代替makeStyle
这样的...