4 javascript css reactjs redux material-ui
https://codesandbox.io/s/2zonj08v5r
const styles = {
root: {
flexGrow: 1
},
colorPrimary: {
color: "green"
}
};
render() {
const { classes } = this.props;
return (
<div className={classes.root}>
<LinearProgress
className={classes.colorPrimary}
variant="determinate"
value={this.state.completed}
Run Code Online (Sandbox Code Playgroud)
小智 9
对于 Material UI v5 (@mui)
<LinearProgress sx={{
backgroundColor: 'white',
'& .MuiLinearProgress-bar': {
backgroundColor: 'green'
}
}}
variant="determinate"
value={10}/>
Run Code Online (Sandbox Code Playgroud)
Ido*_*ev 6
您可以使用 makeStyles 覆盖背景颜色。
在 makeStyles 文件上:
export const useStyles = makeStyles(() => ({
root: {
"& .MuiLinearProgress-colorPrimary": {
backgroundColor: "red",
},
"& .MuiLinearProgress-barColorPrimary": {
backgroundColor: "green",
},
},
})
Run Code Online (Sandbox Code Playgroud)
导入及使用:
import { useStyles } from "./myFile.style";
...
const classes = useStyles();
...
<div className={classes.root}>
<LinearProgress />
</div>
Run Code Online (Sandbox Code Playgroud)
你可以使用例如作为在的答复issue中material ui github项目:https://github.com/mui-org/material-ui/issues/12858#issuecomment-421150158
import React, { Component } from 'react';
import { withStyles } from '@material-ui/core/styles';
import { LinearProgress } from '@material-ui/core';
class ColoredLinearProgress extends Component {
render() {
const { classes } = this.props;
return <LinearProgress {...this.props} classes={{colorPrimary: classes.colorPrimary, barColorPrimary: classes.barColorPrimary}}/>;
}
}
const styles = props => ({
colorPrimary: {
backgroundColor: '#00695C',
},
barColorPrimary: {
backgroundColor: '#B2DFDB',
}
});
export default withStyles(styles)(ColoredLinearProgress);
Run Code Online (Sandbox Code Playgroud)
它完美无缺。
这是因为你的CSS设置不正确,
const styles = {
root: {
flexGrow: 1
},
colorPrimary: {
background: 'green'
}
};
Run Code Online (Sandbox Code Playgroud)
不是:
const styles = {
root: {
flexGrow: 1
},
colorPrimary: {
color: "green",
}
};
Run Code Online (Sandbox Code Playgroud)
希望有帮助!