• react 服务端渲染(ssr) 框架 Next.js


    官方网站:https://nextjs.org/

    deom:https://github.com/xutongbao/hello-next-fetching-data

    index.js:

    import Layout from '../components/MyLayout.js'
    import Link from 'next/link'
    import fetch from 'isomorphic-unfetch'
    
    const Index = (props) => (
      <Layout>
        <h1>Batman TV Shows</h1>
        <ul>
          {props.shows.map(({show}) => (
            <li key={show.id}>
              <Link as={`/p/${show.id}`} href={`/post?id=${show.id}`}>
                <a>{show.name}</a>
              </Link>
            </li>
          ))}
        </ul>
      </Layout>
    )
    
    Index.getInitialProps = async function() {
      const res = await fetch('https://api.tvmaze.com/search/shows?q=batman')
      const data = await res.json()
    
      console.log(`Show data fetched. Count: ${data.length}`)
    
      return {
        shows: data
      }
    }
    
    export default Index

    about.js:

    import Layout from '../components/MyLayout.js'
    
    export default () => (
        <Layout>
           <p>This is the about page</p>
        </Layout>
    )

    post.js:

    import Layout from '../components/MyLayout.js'
    import fetch from 'isomorphic-unfetch'
    
    const Post =  (props) => (
        <Layout>
           <h1>{props.show.name}</h1>
           <p>{props.show.summary.replace(/<[/]?p>/g, '')}</p>
           <img src={props.show.image.medium}/>
        </Layout>
    )
    
    Post.getInitialProps = async function (context) {
      const { id } = context.query
      const res = await fetch(`https://api.tvmaze.com/shows/${id}`)
      const show = await res.json()
    
      console.log(`Fetched show: ${show.name}`)
    
      return { show }
    }
    
    export default Post

    Header.js:

    import Link from 'next/link'
    const linkStyle = {
      marginRight: 15
    }
    
    const Header = () => (
      <div>
        <Link href="/">
          <a style={linkStyle}>Home</a>
        </Link>
        <Link href="/about">
          <a style={linkStyle}>About</a>
        </Link>
      </div>
    )
    
    export default Header

    MyLayout.js:

    import Header from './Header'
    
    const layoutStyle = {
      margin: 20,
      padding: 20,
      border: '1px solid #DDD'
    }
    
    const Layout = (props) => (
      <div style={layoutStyle}>
        <Header />
        {props.children}
      </div>
    )
    
    export default Layout

    server.js:

    const express = require('express')
    const next = require('next')
    
    const dev = process.env.NODE_ENV !== 'production'
    const app = next({ dev })
    const handle = app.getRequestHandler()
    
    app.prepare()
    .then(() => {
      const server = express()
    
      server.get('/p/:id', (req, res) => {
        const actualPage = '/post'
        const queryParams = { id: req.params.id } 
        app.render(req, res, actualPage, queryParams)
      })
    
      server.get('*', (req, res) => {
        return handle(req, res)
      })
    
      server.listen(3000, (err) => {
        if (err) throw err
        console.log('> Ready on http://localhost:3000')
      })
    })
    .catch((ex) => {
      console.error(ex.stack)
      process.exit(1)
    })

    package.json:

    {
      "name": "hello-next",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo "Error: no test specified" && exit 1",
        "dev": "node server.js",
        "build": "next build",
        "start": "NODE_ENV=production node server.js"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "dependencies": {
        "express": "^4.16.4",
        "isomorphic-unfetch": "^3.0.0",
        "next": "^7.0.2",
        "react": "^16.6.3",
        "react-dom": "^16.6.3"
      }
    }
    
  • 相关阅读:
    BZOJ 1565 植物大战僵尸
    BZOJ 1497 最大获利(最大权闭合子图)
    BZOJ 1070 修车(最小费用流)
    BZOJ 2879 NOI2012美食节
    PHPCMS模板里面使用自定义函数
    邓_phpcms_数据库
    邓_ phpcms_
    dedecms====phpcms 区别==[工作]
    邓_html_图片轮播
    dedecms_插件
  • 原文地址:https://www.cnblogs.com/xutongbao/p/11915733.html
Copyright © 2020-2023  润新知