【发布时间】:2019-06-03 01:54:31
【问题描述】:
在项目中使用了 react Material Ui。主题被覆盖,但按钮和输入的颜色没有改变。
这里是创建 Material Ui 主题的theme.js
// @flow
import { createMuiTheme } from '@material-ui/core';
import type
{
Theme,
} from '@material-ui/core';
const theme: Theme = createMuiTheme({
palette: {
primary: {
main: '#ffa300',
light: '#ffd449',
dark: '#c67400',
contrastText: '#000000',
},
secondary: {
main: '#ff8500',
light: '#ffb644',
dark: '#c55600',
contrastText: '#000000',
},
error: {
main: '#A21C2B',
},
},
});
export default theme;
这里是App.js
import React from 'react';
import { ThemeProvider } from '@material-ui/styles';
import CssBaseline from '@material-ui/core/CssBaseline';
import {
Switch,
} from 'react-router-dom';
import theme from './theme';
import Auth from './Auth';
const App = () => (
<ThemeProvider theme={theme}>
<CssBaseline />
<Switch>
<Auth />
</Switch>
</ThemeProvider>
);
export default App;
注意这里使用了新的ThemeProvider,因为稍后会使用钩子。
这是一个示例登录页面
// @flow
import * as React from 'react';
import Avatar from '@material-ui/core/Avatar';
import Button from '@material-ui/core/Button';
import FormControl from '@material-ui/core/FormControl';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import Checkbox from '@material-ui/core/Checkbox';
import Input from '@material-ui/core/Input';
import InputLabel from '@material-ui/core/InputLabel';
import LockIcon from '@material-ui/icons/LockOutlined';
import Paper from '@material-ui/core/Paper';
import Typography from '@material-ui/core/Typography';
import { makeStyles } from '@material-ui/styles';
const useStyles = makeStyles(theme => ({
main: {
width: 'auto',
display: 'block', // Fix IE 11 issue.
marginLeft: theme.spacing.unit * 3,
marginRight: theme.spacing.unit * 3,
[theme.breakpoints.up(400 + theme.spacing.unit * 3 * 2)]: {
width: 400,
marginLeft: 'auto',
marginRight: 'auto',
},
},
paper: {
marginTop: theme.spacing.unit * 8,
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
padding: `${theme.spacing.unit * 2}px\
${theme.spacing.unit * 3}px\
${theme.spacing.unit * 3}px`,
},
avatar: {
margin: theme.spacing.unit,
backgroundColor: theme.palette.secondary.main,
},
form: {
width: '100%', // Fix IE 11 issue.
marginTop: theme.spacing.unit,
},
submit: {
marginTop: theme.spacing.unit * 3,
},
}));
const Login = (props) => {
const classes = useStyles();
return (
<main className={classes.main}>
<Paper className={classes.paper}>
<Avatar className={classes.avatar}>
<LockIcon />
</Avatar>
<Typography component="h1" variant="h5">
Sign in
</Typography>
<form className={classes.form}>
<FormControl margin="normal" required fullWidth>
<InputLabel htmlFor="email">Email Address</InputLabel>
<Input id="email" name="email" autoComplete="email" autoFocus />
</FormControl>
<FormControl margin="normal" required fullWidth>
<InputLabel htmlFor="password">Password</InputLabel>
<Input
name="password"
type="password"
id="password"
autoComplete="current-password"
/>
</FormControl>
<FormControlLabel
control={<Checkbox value="remember" color="primary" />}
label="Remember me"
/>
<Button
type="submit"
fullWidth
variant="contained"
color="primary"
className={classes.submit}
>
Sign in
</Button>
</form>
</Paper>
</main>
);
};
export default Login;
这就是问题所在。按钮和输入的颜色没有改变。但是LockIcon的颜色变成了主题提供的颜色。
【问题讨论】:
-
您是否关注material-ui.com/css-in-js/basics/… 您不需要同时使用两个主题提供程序
-
@JoshWooding 我已经相应地修正了我的答案。
标签: javascript reactjs material-ui