最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Next.js實(shí)現(xiàn)react服務(wù)器端渲染的方法示例

 更新時間:2019年01月06日 11:42:55   作者:mjzhang1993  
這篇文章主要介紹了Next.js實(shí)現(xiàn)react服務(wù)器端渲染的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

說明

實(shí)現(xiàn) 路由跳轉(zhuǎn)、redux

文件版本

  • “next”: “^4.2.3”,
  • “react”: “^16.2.0”,
  • “react-dom”: “^16.2.0”

Next.js GitHub 文檔

項(xiàng)目源碼

使用

Next.js 使用文件體統(tǒng)作為API,可以自動進(jìn)行服務(wù)器端渲染和代碼分割

1. 安裝

yarn add next react react-dom

2. package.json 中添加 npm script

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

3. 創(chuàng)建 /pages 文件夾,其中文件會映射為路由

/pages 文件夾是頂級組件文件夾 其中 /pages/index.js 文件會映射文 / 路由,其他文件根據(jù)文件名映射

目錄結(jié)構(gòu) 映射路由
/pages/index.js /
/pages/about.js /about
/pages/home/index.js /home
/pages/home/about.js /home/about

每一個路由js文件都會 export 一個 React 組件,這個組件可以是函數(shù)式的也可以是通過集成 React.Component 得到的類

export default () => <div>this is index page </div>;

4. 創(chuàng)建 /static 文件夾,存放靜態(tài)資源

靜態(tài)資源文件夾文件會映射到 /static/ 路由下,直接通過 http://localhost:3000/static/test.png 訪問

5. 使用內(nèi)置組件 <head> 定制每個頁面的 head 部分

  import Head from 'next/head'; // 引入內(nèi)置組件

  export default () => (
    <div>
     <Head>
       <title>index page</title>
       <meta name="viewport" content="initial-scale=1.0, width=device-width"/>
     </Head>
     <div>this is index page</div>
    </div>
  );

6. 使用內(nèi)置組件 <Link> 進(jìn)行路由跳轉(zhuǎn)

  import Link from 'next/link';

  export default () => (
    <div>
     <p>this is home index page</p>
     <Link href="/about" rel="external nofollow" rel="external nofollow" >
       <a> to about page</a>
     </Link>
    </div>
  );

更多 Link 使用方式

import React, {Component} from 'react';
import Link from 'next/link';

export default class About extends Component {
  constructor(props) {
   super(props);
  }
  render() {
   // href 值可以是一個對象
   const href = {
     pathname: '/home',
     query: {name: 'test'}
   };

   return (
    <div>
      <p>this is about page </p>
      <img src="/static/test.png" alt="test"/>
      {/* replace 覆蓋歷史跳轉(zhuǎn) */}
      <Link href={href} replace>
      <a>click to home index page</a>
      </Link>
    </div> 
   );
  }
}

7. 使用內(nèi)置 router 方法,手動觸發(fā)路由跳轉(zhuǎn)

next/router 提供一套方法和屬性,幫助確認(rèn)當(dāng)前頁面路由參數(shù),和手動觸發(fā)路由跳轉(zhuǎn)

  import router from 'next/router';
  /*
    router.pathname ==> /home
    router.query ==> {}
    router.route - 當(dāng)前路由
    asPath - 顯示在瀏覽器地址欄的實(shí)際的路由
    push(url, as=url) - 跳轉(zhuǎn)頁面的方法
    replace(url, as=url) - 跳轉(zhuǎn)頁面
  */

更好的方式使用路由 – router 的 withRouter 方法

import Link from 'next/link';
import {withRouter} from 'next/router';

const Home = (props) => {
  // 這里的 props 會得到 {router, url} 兩個屬性
  // router: {push: ƒ, replace: ƒ, reload: ƒ, back: ƒ, prefetch: ƒ, …}
  // url: {query: {…}, pathname: "/home", asPath: "/home?name=test", back: ƒ, push: ƒ, …}
  console.log(props);
  return (
   <div>
     <p>this is home index page </p>
     {/* <Link href="/about" rel="external nofollow" rel="external nofollow" >
      <a> to about page</a>
     </Link> */}
   </div>
  );
}

export default withRouter(Home);

8. 使用 next-redux-wrapper 插件輔助實(shí)現(xiàn) redux

1. 安裝依賴

sudo yarn add next-redux-wrapper redux react-redux redux-devtools-extension redux-thunk

2. 創(chuàng)建 initializeStore.js 一個可以返回 store 實(shí)例的函數(shù)

在這個文件中會完成裝載中間件、綁定reducer、鏈接瀏覽器的redux調(diào)試工具等操作

  import { createStore, applyMiddleware } from 'redux';
  import { composeWithDevTools } from 'redux-devtools-extension'; 
  import thunk from 'redux-thunk';
  import reducer from '../modules/reducers';

  const middleware = [thunk];
  const initializeStore = initialState => {
    return createStore(
       reducer, 
       initialState, 
       composeWithDevTools(applyMiddleware(...middleware))
     );
  };

  export default initializeStore;

3. 創(chuàng)建 reducer , action

與普通 react-redux 項(xiàng)目創(chuàng)建 reducer, action 的方法一致,我把這部分代碼都提取到一個名為 modules的文件夾中

  // /modules/reducers.js
  import { combineReducers } from 'redux';
  import about from './about/reducer';

  // 合并到主reducer
  const reducers = {
    about
  };

  // combineReducers() 函數(shù)用于將分離的 reducer 合并為一個 reducer 
  export default combineReducers(reducers);
  // /modules/about/reudcer.js 
  // /about 頁面的 reducer
  import {
    CHANGE_COUNT
  } from '../types-constant';

  const initialState = {
    count: 0
  };

  const typesCommands = {
    [CHANGE_COUNT](state, action) {
     return Object.assign({}, state, { count: action.msg });
    },
  }

  export default function home(state = initialState, action) {
    const actionResponse = typesCommands[action.type];

    return actionResponse ? actionResponse(state, action) : state;
  }
  // /modules/about/actions.js
  // /about 頁面的 action
  import {
    CHANGE_COUNT
  } from '../types-constant';

  export function changeCount(newCount) {
    return {
     type: CHANGE_COUNT,
     msg: newCount
    };
  }

4. 頁面中使用

需要用到 next-redux-wrapper 提供的 withRedux 高階函數(shù),以及 react-redux 提供的 connect 高階函數(shù)

  import React, { Component } from 'react';
  import withRedux from 'next-redux-wrapper';
  import { connect } from 'react-redux';
  import { bindActionCreators } from 'redux';
  import AboutCom from '../components/About/index';
  import initializeStore from '../store/initializeStore';
  import { changeCount } from '../modules/about/actions';

  class About extends Component {
    constructor(props) {
     super(props);
    }
    render() {
     const { about: { count }, changeCount } = this.props;
     return <AboutCom count={count} changeCount={changeCount} />;
    }
  }

  const connectedPage = connect(
    state => ({ about: state.about }),
    dispatch => ({
     changeCount: bindActionCreators(changeCount, dispatch)
    })
  )(About);

  export default withRedux(initializeStore)(connectedPage);

 更多

查看 github官網(wǎng)

react-next github上一個next架構(gòu)為主實(shí)現(xiàn)React服務(wù)端渲染的模板

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • React使用PropTypes實(shí)現(xiàn)類型檢查功能

    React使用PropTypes實(shí)現(xiàn)類型檢查功能

    這篇文章主要介紹了React高級指引中使用PropTypes實(shí)現(xiàn)類型檢查功能的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-02-02
  • React在定時器中無法獲取狀態(tài)最新值的問題

    React在定時器中無法獲取狀態(tài)最新值的問題

    這篇文章主要介紹了React在定時器中無法獲取狀態(tài)最新值的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • 使用react-virtualized實(shí)現(xiàn)圖片動態(tài)高度長列表的問題

    使用react-virtualized實(shí)現(xiàn)圖片動態(tài)高度長列表的問題

    一般我們在寫react項(xiàng)目中,同時渲染很多dom節(jié)點(diǎn),會造成頁面卡頓, 空白的情況。為了解決這個問題,今天小編給大家分享一篇教程關(guān)于react-virtualized實(shí)現(xiàn)圖片動態(tài)高度長列表的問題,感興趣的朋友跟隨小編一起看看吧
    2021-05-05
  • React?Fiber?鏈表操作及原理示例詳解

    React?Fiber?鏈表操作及原理示例詳解

    這篇文章主要為大家介紹了React?Fiber?鏈表操作原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • React使用useImperativeHandle自定義暴露給父組件的示例詳解

    React使用useImperativeHandle自定義暴露給父組件的示例詳解

    useImperativeHandle?是?React?提供的一個自定義?Hook,用于在函數(shù)組件中顯式地暴露給父組件特定實(shí)例的方法,本文將介紹?useImperativeHandle的基本用法、常見應(yīng)用場景,需要的可以參考下
    2024-03-03
  • React 如何使用時間戳計(jì)算得到開始和結(jié)束時間戳

    React 如何使用時間戳計(jì)算得到開始和結(jié)束時間戳

    這篇文章主要介紹了React 如何拿時間戳計(jì)算得到開始和結(jié)束時間戳,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-09-09
  • React中如何引入Angular組件詳解

    React中如何引入Angular組件詳解

    這篇文章主要給大家介紹了關(guān)于React中如何引入Angular組件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • React組件的應(yīng)用介紹

    React組件的應(yīng)用介紹

    React組件分為函數(shù)組件與class組件;函數(shù)組件是無狀態(tài)組件,class稱為類組件;函數(shù)組件只有props,沒有自己的私有數(shù)據(jù)和生命周期函數(shù);class組件有自己私有數(shù)據(jù)(this.state) 和 生命周期函數(shù)
    2022-09-09
  • react數(shù)據(jù)管理中的setState與Props詳解

    react數(shù)據(jù)管理中的setState與Props詳解

    setState?是?React?中用于更新組件狀態(tài)(state)的方法,本文給大家介紹react數(shù)據(jù)管理中的setState與Props知識,感興趣的朋友跟隨小編一起看看吧
    2023-10-10
  • React實(shí)現(xiàn)雙滑塊交叉滑動

    React實(shí)現(xiàn)雙滑塊交叉滑動

    這篇文章主要為大家詳細(xì)介紹了React實(shí)現(xiàn)雙滑塊交叉滑動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09

最新評論

秦安县| 罗城| 藁城市| 聊城市| 乌鲁木齐县| 湖北省| 进贤县| 盐边县| 商南县| 准格尔旗| 郁南县| 昌都县| 体育| 屯昌县| 龙山县| 繁昌县| 蒙自县| 金门县| 繁昌县| 友谊县| 漳州市| 三穗县| 汝南县| 温宿县| 札达县| 洛隆县| 九龙县| 板桥市| 泽州县| 宽甸| 蒙山县| 郧西县| 揭西县| 托克逊县| 泽普县| 饶河县| 宾川县| 大田县| 苏尼特右旗| 西乡县| 道孚县|