简体   繁体   English

Material-UI 样式:将功能组件转换为类组件

[英]Material-UI styles: convert functional component to class component

So I try the following code to convert a functional component to the classical component, it kinda worked, no error but styles are not applied .所以我尝试使用以下代码将功能组件转换为经典组件,它有点工作,没有错误但没有应用样式

import { makeStyles } from '@material-ui/core/styles';
import { withStyles } from '@material-ui/core/styles';
const playtheMusic = () => {
    pauseMusic();
};
const pausetheMusic = () => {
    pauseMusic();
};
const useStyles = makeStyles(theme => ({
    text: {
        padding: 50
    },
    paper: {
        paddingBottom: 50
    },
    list: {
        marginBottom: theme.spacing(2)
    },
    subheader: {
        backgroundColor: theme.palette.background.paper
    },
    appBar: {
        top: 'auto',
        bottom: 0,
        backgroundColor: '#282828',
        padding: '15px'
    },
    grow: {
        flexGrow: 1
    }
}));
class BottomAppBar extends React.Component {
    // const classes = useStyles();
    render() {
        const { classes } = this.props;
        return (
            <div>
                <AppBar position="fixed" className={classes.appBar}>
                    <div style={{ display: 'flex', alignItems: 'center', alignContent: 'center' }}>
                        <div>
                            <Typography style={{ fontSize: 15 }}> Stress Out </Typography>
                            <br />
                            <Typography style={{ fontSize: 12, color: '#B3B3B3' }}>
                                Twenty One Pilots
                            </Typography>
                        </div>
                        <div className={classes.grow} />
                        <div>
                            <IconButton style={{ color: 'white' }}>
                                <ShuffleIcon />
                            </IconButton>
                            <IconButton style={{ color: 'white' }}>
                                <SkipPreviousRoundedIcon style={{ fontSize: 30 }} />
                            </IconButton>
                            <IconButton onClick={pausetheMusic} style={{ color: 'white' }}>
                                <PauseCircleOutlineRoundedIcon style={{ fontSize: 46 }} />
                                <PlayCircleOutlineIcon style={{ fontSize: 46, display: 'none' }} />
                            </IconButton>
                            <IconButton style={{ color: 'white' }}>
                                <SkipNextRoundedIcon style={{ fontSize: 30 }} />
                            </IconButton>
                            <IconButton style={{ color: 'white' }}>
                                <RepeatIcon />
                            </IconButton>
                        </div>
                        <div className={classes.grow} />
                        <div>
                            <IconButton style={{ color: 'white' }}>
                                <VolumeUpIcon />
                            </IconButton>
                        </div>
                    </div>
                </AppBar>
            </div>
        );
    }
}
export default withStyles(useStyles)(BottomAppBar);

also, there is a problem with StackOverflow.此外,StackOverflow 也存在问题。 it says "It looks like your post is mostly code; please add some more details".它说“看起来您的帖子主要是代码;请添加更多详细信息”。 that's the reason I'm writing some unnecessary things XD you can skip it.这就是我写一些不必要的东西的原因 XD 你可以跳过它。

Thanks for reading.谢谢阅读。 have a good day <3祝你有美好的一天 <3

A common approach for material-ui component styling : material-ui 组件样式的常用方法:

Classical古典

withStyles (High order function) + createStyles withStyles(高阶函数)+ createStyles

Functional功能性

useStyles (hooks) + makeStyles useStyles(钩子)+ makeStyles


In your code, you shall not use the hooks useStyles inside withStyle , hooks shouldn't be used inside any classical component ,在代码中,你不得使用挂钩useStyleswithStyle ,钩不应该任何内部使用classical component

  • Wrong here错在这里
export default withStyles(useStyles)(BottomAppBar);
  • Right example正确的例子
import { withStyles, createStyles } from "@material-ui/core/styles";
const styles = theme => createStyles({
  root: {
  },
  // ...
});
...
const { classes } = this.props;
...
export default withStyles(styles)(App);

Online sample for both classical component and functional component styling classical componentfunctional component样式的在线示例

编辑 staging-framework-3h4m8

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Class 组件中使用 Material-UI HOC - Use Material-UI HOC in Class Component React js:将 Material-UI 功能代码转换为 class 组件会抛出 typerror - React js: Converting Material-UI functional code into class component throws typerror 使用 React.js 和 Material-UI 将数据渲染为功能组件 - Render data as a functional component with React.js and Material-UI 自定义 React 组件样式被 Material-UI 样式覆盖 - Custom React Component styles being overwritten by Material-UI style 使用 material-ui 更改父组件中的子组件样式 - Changing child-component styles in parent with material-ui 如何导出具有 2 个样式对象的 react material-ui 组件? (材质-ui V1) - How can I export a react material-ui component with 2 styles object? (material-ui V1) Material UI 样式在组件中不起作用(警告:`@material-ui/styles` 的几个实例) - Material UI styles not working in component (warning: several instances of `@material-ui/styles`) 如何正确地将引用从功能组件转发到 material-ui 组件 - How to properly forward a ref to a material-ui component from a functional component 如何在不丢失样式的情况下将 material-ui react 组件包装到 Web 组件? - How can I wrap a material-ui react component to a Web Component without losing the styles? React和Material-Ui在.js中定义类,并将其作为字符串传递给组件 - React and Material-Ui define class in .js and pass it as string to a component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM