如何使用 React + Next.js 在 Material UI 中使用 Theme?

dmj*_*mjy 3 reactjs material-design material-ui next.js

我创建了如下所示的三个文件。

的颜色Paper根据 中palette.type定义的值而变化theme.js

带有palette.type的页面:'dark'

带有palette.type的页面:'dark'

带有palette.type的页面:'light'

带有palette.type的页面:'light'

但是,我不明白该变量theme是如何工作的。我console.log(theme)index.js. palette.type: 'light'无论 中的值如何,两者都显示theme.js

console.log 的结果

console.log 的结果

我如何传递themeindex.js(不导入主题)?

components/Layout.js - 将相同布局应用于所有页面的共享组件

import React from 'react';
import { MuiThemeProvider } from '@material-ui/core/styles';
import { theme } from '../utils/theme';


class Layout extends React.Component {
    render() {
        return(
            <MuiThemeProvider theme={theme}>
                {this.props.children}
            </MuiThemeProvider>
        )
    }
}

export default Layout;
Run Code Online (Sandbox Code Playgroud)

pages/index.js - 一个特定的页面

import Layout from '../components/Layout';
import { makeStyles, createStyles, useTheme, Paper } from '@material-ui/core';

const useStyles = makeStyles(theme => {
    console.log('inside makeStyles');
    console.log(theme);
    return createStyles({
        container: {
            margin: '5px 5px 50px 5px'
        }
    })
});

export default function Index(props) {
    const classes = useStyles(props);
    const theme = useTheme();
    console.log('inside Index')
    console.log(theme);

    return(
        <Layout>
            <h2>About</h2>
            <Paper className={classes.container}>
                <p>Sample test</p>
            </Paper>
            <Paper className={classes.container}>
                <p>Sample test2</p>
            </Paper>
        </Layout>
    )
};
Run Code Online (Sandbox Code Playgroud)

utils/theme.js - 要写入的单独文件theme

import { createMuiTheme } from '@material-ui/core/styles';

export const theme = createMuiTheme({
    palette: {
        type: 'dark'
    }
});
Run Code Online (Sandbox Code Playgroud)

Nik*_*lev 5

尝试<MuiThemeProvider theme={theme}>Layout.js移动到_app.js

_app.js

import React from 'react';
import PropTypes from 'prop-types';
import Head from 'next/head';
import { ThemeProvider } from '@material-ui/core/styles';
import CssBaseline from '@material-ui/core/CssBaseline';
import theme from '../src/theme';

export default function MyApp(props) {
const { Component, pageProps } = props;

React.useEffect(() => {
    // Remove the server-side injected CSS.
    const jssStyles = document.querySelector('#jss-server-side');
    if (jssStyles) {
        jssStyles.parentElement.removeChild(jssStyles);
    }
}, []);

return (
    <React.Fragment>
    <Head>
        <title>My page</title>
        <meta name="viewport" content="minimum-scale=1, initial-scale=1, width=device-width" />
    </Head>
    <ThemeProvider theme={theme}>
        {/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
        <CssBaseline />
        <Component {...pageProps} />
    </ThemeProvider>
    </React.Fragment>
);
}

MyApp.propTypes = {
    Component: PropTypes.func.isRequired,
    pageProps: PropTypes.object.isRequired,
};
Run Code Online (Sandbox Code Playgroud)

主题应该适用于所有组件,您应该darkconsole.log()打印中看到。

官方 Material-UI + Next.js 示例

如果所有页面共享相同的布局,您可以Layout完全删除组件。