[React] Create & Deploy a Universal React App using Zeit Next
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.
[React] Create & Deploy a Universal React App using Zeit Next的更多相关文章
- [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 ...
- 使用React Native来撰写跨平台的App
React Native 是一个 JavaScript 的框架,用来撰写实时的.可原生呈现 iOS 和 Android 的应用.其是基于 React的,而 React 是 Facebook 的用于构建 ...
- [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 ...
- 当今流行的 React.js 适用于怎样的 Web App?
外村 和仁(株式会社 ピクセルグリッド) React.js是什么? React.js是Facebook开发的框架. http://facebook.github.io/react/ 官网上的描述是「 ...
- 2、手把手教React Native实战之从React到RN
###React简介 RN是基于React设计,了解React有助于我们开发RN应用: React希望将功能分解化,让开发变得像搭积木一样,快速而且可维护 React主要有如下3个特点: *作为UI( ...
- 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 ...
- 使用react搭建组件库:react+typescript+storybook
前期准备 1. 初始化项目 npx create-react-app react-components --template typescript 2. 安装依赖 使用哪种打包方案:webpack/r ...
- React Canvas:高性能渲染 React 组
React Canvas 提供了使用 Canvas 渲染移动 Web App 界面的能力,替代传统的 DOM 渲染,具有更接近 Native App 的使用体验.React Canvas 提供了一组标 ...
- react实战项目开发(2) react几个重要概念以及JSX语法
前言 前面我们已经学习了利用官方脚手架搭建一套可以应用在生产环境下的React开发环境.那么今天这篇文章主要先了解几个react重要的概念,以及讲解本文的重要知识JSX语法 React重要概念 [思想 ...
随机推荐
- 洛谷P1852 奇怪的字符串
题目描述 输入两个01串,输出它们的最长公共子序列的长度 输入输出格式 输入格式: 一行,两个01串 输出格式: 最长公共子序列的长度 输入输出样例 输入样例#1: 复制 01010101010 00 ...
- eclipse创建maven
第一步: 第二步 第三步: 第四步: 第五步: 第六步: <?xml version="1.0" encoding="UTF-8"?> <we ...
- python学习一,python基本语法
python基本语法: 1.python基本语句结构: 首先,在其他的语言中,比如java,c++,c#等,没写完一行语句之后,都需要在语句的末尾加一个分号,表示该语句结束,但是在python中,我们 ...
- 微信小程序简单常见首页demo
wxml <view class='index-contier'> <view class="index-left"> <view>电池剩余&l ...
- Spring学习总结(8)——25个经典的Spring面试问答
1.什么是Spring框架?Spring框架有哪些主要模块? Spring框架是一个为Java应用程序的开发提供了综合.广泛的基础性支持的Java平台.Spring帮助开发者解决了开发中基础性的问题, ...
- PatentTips - Interrupt redirection for virtual partitioning
BACKGROUND The present disclosure relates to the handling of interrupts in a environment that utiliz ...
- jsp+tomcat+ 创建project 配置project
*如今我们已经下载到了 tomcat 7.0+ eclipse for java ee 直接解压,打开eclipse. 接下来是步骤: eclipse 打开的界面.空空如也 ! ..* 点击 file ...
- 跨域请求发送不了cookie问题: AJAX跨域请求JS配置和服务器端配置
1.ajax是同步方式 $.ajax({ type: "post", url:url, async:false, data:datatosend, dataType:"j ...
- C# List 复制克隆副本
[C#技术] C# List 复制克隆副本数字人 发表于:2012-8-28 18:02:49废话不多说,看代码: 方法一: List<string> t = new List<st ...
- 洛谷 P2693 [USACO1.3]号码锁 Combination Lock
P2693 [USACO1.3]号码锁 Combination Lock 题目描述 农夫约翰的奶牛不停地从他的农场中逃出来,导致了很多损害.为了防止它们再逃出来,他买了一只很大的号码锁以防止奶牛们打开 ...