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. VS:&quot;64位调试操作花费的时间比预期要长&quot;的一解决途径

    解决的方法之中的一个: 在命令提示符那里打入例如以下命令: netsh winsock reset catalog netsh int ip reset reset.log hit 重新启动电脑后,就 ...

  2. Android app : use html or native?

    Android app可分为两种:网络(html)应用程序和原生(native)应用程序 首先,我们先来讨论下如何判断一个app是html实现还是native实现. 设置-->>开发者选项 ...

  3. D. Dreamoon and Sets(Codeforces Round #272)

    D. Dreamoon and Sets time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. windows 2016 配置 VNC 服务

    windows 2016 配置 VNC 服务 下载windows版 https://www.realvnc.com/download/vnc/ 安装时勾选 vncserver 进入 "C:\ ...

  5. Button- 自定义控件添加自定义属性

    今天自定义了一个button按钮,需要添加一个属性,具体步骤如下 1.新属性的信息设定:在values目录下添加attrs.xml文件,在里面添加属性信息 <?xml version=" ...

  6. ::的类名前有个 & ,什么意思?

    转载自  http://www.imooc.com/qadetail/93985 MazePerson &MazePerson::setPersonPosition(int coordinat ...

  7. jqXHR对象

    //$.ajax()返回的对象就是jqXHR对象 var jqXHR = $.ajax({ type:'post', url:'test.php', data:$('form').serialize( ...

  8. bind()和trigger()额外数据

    $(function(){ $('input').click(function(e,data1,data2,data3,data4){ alert(data1 + '|' + data2 + '|' ...

  9. Boost解析xml——xml写入

    <?xml version="1.0" encoding="utf-8"?> <Config> <Item name=" ...

  10. Android 继承framelayout,实现ScrollView 和 HorizontalScrollView 的效果

    有些项目,需要让控件或者布局进行水平和垂直同时能拖拽,当然,ScrollView 和 HorizontalScrollView 的结合写法是一种写法.但是,这么写用户体验效果不佳,会有迟钝感,因此推荐 ...