Hay*_*yan 0 javascript reactjs material-ui
在反应材料 Ui 的项目中使用。主题被覆盖,但按钮和输入的颜色不会改变。
这theme.js是创建 Material Ui 主题的地方
// @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;
Run Code Online (Sandbox Code Playgroud)
这是 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;
Run Code Online (Sandbox Code Playgroud)
请注意 new 的使用,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;
Run Code Online (Sandbox Code Playgroud)
这就是问题所在。按钮和输入的颜色没有改变。但是 的颜色LockIcon更改为主题提供的颜色。
更新
下面的答案特定于@material-ui/styles与 3.x 版本的 Material-UI 一起使用。bootstrapMuiStyles.jsMaterial-UI v4 不再需要这些安装步骤 ( )。
最初我建议也用MuiThemeProvider. 这有效,但这是错误的解决方案,并且在尝试使用新@material-ui/styles软件包时可能会导致更多问题。根据 Josh Wooding 的评论,然后阅读文档中的详细信息并查看一些代码,下面是符合 Josh 建议的解决方案。摘录来自我的 CodeSandbox,因此由于简化/更改(例如不使用类型)而存在一些差异,我对您的代码做了一个工作示例。最重要的部分是刚刚index.js和bootstrapMuiStyles.js-尤其是进口的排序。install直接从导入方法是行不通的index.js,因为这将迫使您install()在导入后调用App.js。
index.js
import React from "react";
import ReactDOM from "react-dom";
// It is important for this import to be before the import of App
// so that no other Material-UI imports happen before the install
import "./bootstrapMuiStyles";
import App from "./App";
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Run Code Online (Sandbox Code Playgroud)
bootstrapMuiStyles.js
import { install } from "@material-ui/styles";
install();
Run Code Online (Sandbox Code Playgroud)
App.js
import React from "react";
import { ThemeProvider } from "@material-ui/styles";
import CssBaseline from "@material-ui/core/CssBaseline";
import theme from "./theme";
import Login from "./Login";
const App = () => (
<ThemeProvider theme={theme}>
<CssBaseline />
<Login />
</ThemeProvider>
);
export default App;
Run Code Online (Sandbox Code Playgroud)
然后Login.js是您问题中几乎完全相同的代码副本。
你可以在这里看到这个工作:
| 归档时间: |
|
| 查看次数: |
5089 次 |
| 最近记录: |