• nextjs-demo


    这个是根据nextjs服务端渲染做的一个小demo

    关键性代码,主要是控制模态框显示隐藏
    关于index页面

    //index.js
    /* eslint-disable jsx-a11y/anchor-is-valid */
    
    import React from 'react';
    import PropTypes from 'prop-types';
    import Button from '@material-ui/core/Button';
    import Dialog from '@material-ui/core/Dialog';
    import DialogTitle from '@material-ui/core/DialogTitle';
    import DialogContent from '@material-ui/core/DialogContent';
    import DialogContentText from '@material-ui/core/DialogContentText';
    import DialogActions from '@material-ui/core/DialogActions';
    import Typography from '@material-ui/core/Typography';
    import { withStyles } from '@material-ui/core/styles';
    import Link from 'next/link';
    
    const styles = theme => ({
      root: {
        textAlign: 'center',
        paddingTop: theme.spacing.unit * 20,
      },
    });
    
    class Index extends React.Component {
      state = {
        open: false,
      };
    
      handleClose = () => {
        this.setState({
          open: false,
        });
      };
    
      handleClick = () => {
        this.setState({
          open: true,
        });
      };
    
      render() {
        const { classes } = this.props;
        const { open } = this.state;
    
        return (
          <div className={classes.root}>
            <Dialog open={open} onClose={this.handleClose}>
              <DialogTitle>Super Secret Password</DialogTitle>
              <DialogContent>
                <DialogContentText>1-2-3-4-5</DialogContentText>
              </DialogContent>
              <DialogActions>
                <Button color="primary" onClick={this.handleClose}>
                  OK
                </Button>
              </DialogActions>
            </Dialog>
            <Typography variant="h4" gutterBottom>
              Material-UI
            </Typography>
            <Typography variant="subtitle1" gutterBottom>
              example project
            </Typography>
            <Typography gutterBottom>
              <Link href="/about">
                <a>Go to the about page</a>
              </Link>
            </Typography>
            <Button variant="contained" color="secondary" onClick={this.handleClick}>
              Super Secret Password
            </Button>
          </div>
        );
      }
    }
    
    Index.propTypes = {
      classes: PropTypes.object.isRequired,
    };
    
    export default withStyles(styles)(Index);
    
    

    关于about页面

    代码如下:

    //about.js
    /* eslint-disable jsx-a11y/anchor-is-valid */
    
    import React from 'react';
    import PropTypes from 'prop-types';
    import Button from '@material-ui/core/Button';
    import Typography from '@material-ui/core/Typography';
    import { withStyles } from '@material-ui/core/styles';
    import Link from 'next/link';
    const styles = theme => ({
      root: {
        textAlign: 'center',
        paddingTop: theme.spacing.unit * 20,
      },
    });
    
    function About(props) {
      const { classes } = props;
      console.log('classes..props',classes);
    
      return (
        <div className={classes.root}>
          <Typography variant="h4" gutterBottom>
            Material-UI
          </Typography>
          <Typography variant="subtitle1" gutterBottom>
            about page
          </Typography>
          <Typography gutterBottom>
            <Link href="/">
              <a>Go to the main page</a>
            </Link>
          </Typography>
          <Button variant="contained" color="primary">
            Do nothing button
          </Button>
        </div>
      );
    }
    
    About.propTypes = {
      classes: PropTypes.object.isRequired,
    };
    
    export default withStyles(styles)(About);
    
    
    //app.js
    import React from 'react';
    import App, { Container } from 'next/app';
    import Head from 'next/head';
    import { MuiThemeProvider } from '@material-ui/core/styles';
    import CssBaseline from '@material-ui/core/CssBaseline';
    import JssProvider from 'react-jss/lib/JssProvider';
    import getPageContext from '../src/getPageContext';
    
    class MyApp extends App {
      constructor() {
        super();
        this.pageContext = getPageContext();
      }
    
      componentDidMount() {
        // Remove the server-side injected CSS.
        const jssStyles = document.querySelector('#jss-server-side');
        if (jssStyles && jssStyles.parentNode) {
          jssStyles.parentNode.removeChild(jssStyles);
        }
      }
    
      render() {
        const { Component, pageProps } = this.props;
        return (
          <Container>
            <Head>
              <title>My page</title>
            </Head>
            {/* Wrap every page in Jss and Theme providers */}
            <JssProvider
              registry={this.pageContext.sheetsRegistry}
              generateClassName={this.pageContext.generateClassName}
            >
              <MuiThemeProvider
                theme={this.pageContext.theme}
                sheetsManager={this.pageContext.sheetsManager}
              >
                {/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
                <CssBaseline />
                {/* Pass pageContext to the _document though the renderPage enhancer
                    to render collected styles on server-side. */}
                <Component pageContext={this.pageContext} {...pageProps} />
              </MuiThemeProvider>
            </JssProvider>
          </Container>
        );
      }
    }
    
    export default MyApp;
    
    
  • 相关阅读:
    Apple的App Analytics统计平台你必须知道的Q&A整理与翻译
    WWDC2014总结---For产品经理们
    AppStore占坑注意事项
    Mac上的终端(Terminal)启动缓慢
    iOS推送失败的可能问题汇总
    Mac OS X 10.9 Mavericks安装后,Xcode调试时模拟器黑屏的处理方法
    MySql批处理的小窍门:排行榜类数据生成
    升级OSX 10.9 Mavericks后,会导致Finder始终无响应的一个问题
    拉面馆中的移动互联网——无线KPI探讨
    Weak is not weak,Strong is not strong
  • 原文地址:https://www.cnblogs.com/smart-girl/p/10796761.html
Copyright © 2020-2023  润新知