GithubHelp home page GithubHelp logo

kszitt / next-initial-request Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 3 KB

next.js框架服务端数据请求、客户端获取数据插件

JavaScript 100.00%
nextjs next request initial server client

next-initial-request's Introduction

语言

English

描述

next.js框架数据请求、获取插件。

安装

npm install next-initial-request --save-dev

创建http请求(举例)

async function GetActivityById(id){
  let response = await axios.get(`/api/v1/activity/info/${id}`);

  return response.data;
}
async function getActivity(params){
  let response = await axios.get(`/api/v1/activity/all`, {params});

  return response.data;
}

引入

import NextHttp from "next-initial-request"
// or
const NextHttp = require("next-initial-request");

服务端请求数据

static async getInitialProps ({req, pathname, query}) {
  let props = {};

  if(req){
    // 并发
    let results = await Promise.all([
      getActivityById(query.id),
      getActivity(),
    ]);
    /* // 同步请求(根据接口的数据请求接口时,可以用这种方式)
    let results = [];
    results[0] = await getActivityById(query.id);
    results[2] = await getActivity(); */

    props.initProps = NextHttp.initProps(results, ["activity", "activityAll"]);
  }

  return props;
}

客户端获取数据

async getActivity(){
    let params = {
        page: 1,
        perpage: 5
      },
      // NextHttp.getProps()只会在服务端渲染一次,客户端直接请求
      data = NextHttp.getProps(this, "activityAll") || await getActivity(params),
      activityAll = [];

    if(data.code === 200){
      activityAll = data.items;
    }

    this.setState({
      activityAll
    })
  }

.initProps(results, keys)

服务端请求数据保存到props

  • results{ array|object } 要保存的数据,必须
  • keys{ string:array | string } 数据绑定的key值,必须

.getProps(this, key)

props中获取数据(只会在服务端请求一次)

  • this{ object } 传入当前组件的this,必须
  • key{ string:array | string } 从props中根据key提取数据,必须

next-initial-request's People

Contributors

kszitt avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.