如何使用makeStyles设置组件样式并在Material UI中仍然具有生命周期方法? [英] How to style components using makeStyles and still have lifecycle methods in Material UI?
问题描述
每当尝试将makeStyles()
与具有生命周期方法的组件一起使用时,都会出现以下错误:
I get the below error whenever I try to use makeStyles()
with a component with lifecycle methods:
无效的挂钩调用.挂钩只能在功能组件的主体内部调用.发生这种情况可能是由于以下原因之一:
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:
- 您可能使用的React和渲染器版本不匹配(例如React DOM)
- 您可能正在违反挂钩规则
- 您可能在同一应用中拥有多个React副本
- You might have mismatching versions of React and the renderer (such as React DOM)
- You might be breaking the Rules of Hooks
- You might have more than one copy of React in the same app
下面是产生此错误的一小段代码示例.其他示例也将类分配给子项.我在MUI的文档中找不到任何显示使用makeStyles
的其他方法并能够使用生命周期方法的东西.
Below is a small example of code that produces this error. Other examples assign classes to child items as well. I can't find anything in MUI's documentation that shows other ways to use makeStyles
and have the ability to use lifecycle methods.
import React, { Component } from 'react';
import { Redirect } from 'react-router-dom';
import { Container, makeStyles } from '@material-ui/core';
import LogoButtonCard from '../molecules/Cards/LogoButtonCard';
const useStyles = makeStyles(theme => ({
root: {
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
},
}));
const classes = useStyles();
class Welcome extends Component {
render() {
if (this.props.auth.isAuthenticated()) {
return <Redirect to="/" />;
}
return (
<Container maxWidth={false} className={classes.root}>
<LogoButtonCard
buttonText="Enter"
headerText="Welcome to PlatformX"
buttonAction={this.props.auth.login}
/>
</Container>
);
}
}
export default Welcome;
推荐答案
我们最终使用类组件停止了工作,并创建了功能组件使用生命周期方法的Hooks API .这使您仍然可以将makeStyles()
与生命周期方法一起使用
What we ended up doing is stopped using the class components and created Functional Components, using useEffect()
from the Hooks API for lifecycle methods. This allows you to still use makeStyles()
with Lifecycle Methods without adding the complication of making Higher-Order Components. Which is much simpler.
示例:
import React, { useEffect, useState } from 'react';
import axios from 'axios';
import { Redirect } from 'react-router-dom';
import { Container, makeStyles } from '@material-ui/core';
import LogoButtonCard from '../molecules/Cards/LogoButtonCard';
const useStyles = makeStyles(theme => ({
root: {
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
margin: theme.spacing(1)
},
highlight: {
backgroundColor: 'red',
}
}));
// Highlight is a bool
const Welcome = ({highlight}) => {
const [userName, setUserName] = useState('');
const [isAuthenticated, setIsAuthenticated] = useState(true);
const classes = useStyles();
useEffect(() => {
axios.get('example.com/api/username/12')
.then(res => setUserName(res.userName));
}, []);
if (!isAuthenticated()) {
return <Redirect to="/" />;
}
return (
<Container maxWidth={false} className={highlight ? classes.highlight : classes.root}>
<LogoButtonCard
buttonText="Enter"
headerText={isAuthenticated && `Welcome, ${userName}`}
buttonAction={login}
/>
</Container>
);
}
}
export default Welcome;
这篇关于如何使用makeStyles设置组件样式并在Material UI中仍然具有生命周期方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!