In this lesson, we'll use next to create a universal React application with no configuration. We'll create page components that will render on the server if accessed directly, but function as you would expect in the client. We'll use the routing capabilities included with next to create links between the components using pushState and we'll incorporate our own React component. Finally, we'll deploy the application to now with a simple command in the terminal.

Setup:

npm init -y

Install:

npm i --save-dev next

Scripts:

  "scripts": {
"dev": "next",
"start": "next start",
"build": "next build"
},

Create pages/index.js file:

import React from 'react';

export default () => (
<div>
<h1>Hello next!!</h1>
</div>
)

Run:

npm run dev

Then you can see the index.js page on the localhost:3000.

Add another page:

import React from 'react';

export default class About extends React.Component {

    static getInitialProps({req}) {
const name = req ? 'server': 'client';
return {
name: name
};
} render() {
return (
<div>
<h1>ABOUT PAGE</h1>
<span>
This page is rendering from {this.props.name}
</span>
</div>
);
}
}

This can show whether the page is rendered from the server side or clinet side.

Create a nav component to do the rounting:

// compoennts/nav.js

import React from 'react';
import Link from 'next/link'; export default () => (
<div>
<Link href="/">Home</Link>
{' '}
<Link href="/about">About</Link>
</div>
)

Then import to home page and about page.

// index.js

import React from 'react';
import Link from 'next/link'; import Nav from '../components/nav'; export default () => (
<div>
<h1>Hello next!!</h1>
<Nav></Nav>
</div>
)

Then we can create an .npmignore to ignore the .next folder in the root.

Last using 'now' to deploy the page to now.

Github

[React] Create & Deploy a Universal React App using Zeit Next的更多相关文章

  1. [React] Create component variations in React with styled-components and "extend"

    In this lesson, we extend the styles of a base button component to create multiple variations of but ...

  2. 使用React Native来撰写跨平台的App

    React Native 是一个 JavaScript 的框架,用来撰写实时的.可原生呈现 iOS 和 Android 的应用.其是基于 React的,而 React 是 Facebook 的用于构建 ...

  3. [React] Create a Persistent Reference to a Value Using React useRef Hook

    The useRef is a hook for creating values that persist across renders. In this lesson we'll learn how ...

  4. 当今流行的 React.js 适用于怎样的 Web App?

    外村 和仁(株式会社 ピクセルグリッド)  React.js是什么? React.js是Facebook开发的框架. http://facebook.github.io/react/ 官网上的描述是「 ...

  5. 2、手把手教React Native实战之从React到RN

    ###React简介 RN是基于React设计,了解React有助于我们开发RN应用: React希望将功能分解化,让开发变得像搭积木一样,快速而且可维护 React主要有如下3个特点: *作为UI( ...

  6. React Tutorial: Basic Concept Of React Component---babel, a translator

    Getting started with react.js: basic concept of React component 1 What is React.js React, or React.j ...

  7. 使用react搭建组件库:react+typescript+storybook

    前期准备 1. 初始化项目 npx create-react-app react-components --template typescript 2. 安装依赖 使用哪种打包方案:webpack/r ...

  8. React Canvas:高性能渲染 React 组

    React Canvas 提供了使用 Canvas 渲染移动 Web App 界面的能力,替代传统的 DOM 渲染,具有更接近 Native App 的使用体验.React Canvas 提供了一组标 ...

  9. react实战项目开发(2) react几个重要概念以及JSX语法

    前言 前面我们已经学习了利用官方脚手架搭建一套可以应用在生产环境下的React开发环境.那么今天这篇文章主要先了解几个react重要的概念,以及讲解本文的重要知识JSX语法 React重要概念 [思想 ...

随机推荐

  1. hdu1874 畅通project续 最短路 floyd或dijkstra或spfa

    Problem Description 某省自从实行了非常多年的畅通project计划后.最终修建了非常多路.只是路多了也不好,每次要从一个城镇到还有一个城镇时,都有很多种道路方案能够选择.而某些方案 ...

  2. AsyncTask源代码翻译

    前言: /** <p>AsyncTask enables proper and easy use of the UI thread. This class allows to perfor ...

  3. Aruba 云服务代金券

    Aruba 云服务代金券 Aruba Cloud是欧洲的一家VPS供应商,他家的VPS是基于VMware的,有英国.法国.德国.意大利.捷克5处数据中心,每个月最低1欧元,非欧洲企业客户可以免税 这里 ...

  4. java移位操作符注意的问题

    如果对char,byte或者short类型的数值进行移位处理,那么在移位进行之前,他们会被转为int类型, 并且所得到的结果也是一个int型. 若对long类型的数值进行处理所得到的结果也是long. ...

  5. vmware workstation安装windows server 2019

    提示需要输入密钥,选择[我没有密钥] 选择有桌面体验的 自定义 新建C盘 100G 选择100G的主分区,下一步 PS:未分配的空间,先不理,安装完系统,进桌面,再分配 开始安装.接下来就是等. 等. ...

  6. 阅读笔记——Web应用程序

    Web应用程序与DD文件 Web应用程序 web应用程序是一种可以通过Web访问的应用程序.Web应用程序最大的好处是永和很容易访问应用程序.用户只需要有浏览器即可,不需要安装其他任何软件.一个Web ...

  7. 【习题 7-9 UVA-1604】Cubic Eight-Puzzle

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] IDA* 保证这次移动的方格不和前一次重复. 然后加一个8数码的剪枝就行了. ->看看当前状态和目标状态有多少个地方是不一样的 ...

  8. Shiro学习总结(3)——Apache Shiro身份认证

    身份验证,即在应用中谁能证明他就是他本人.一般提供如他们的身份ID一些标识信息来表明他就是他本人,如提供身份证,用户名/密码来证明. 在shiro中,用户需要提供principals (身份)和cre ...

  9. 洛谷 P2240 数的计数数据加强版

    P2240 数的计数数据加强版 题目背景 无 题目描述 我们要求找出具有下列性质数的个数(包含输入的自然数n): 先输入一个自然数n(n<=1500001),然后对此自然数按照如下方法进行处理: ...

  10. HDU 3584 Cube (三维树状数组)

    Problem Description Given an N*N*N cube A, whose elements are either 0 or 1. A[i, j, k] means the nu ...