PreSWR - Preload your useSWR calls for Server Side Rendering

栏目: IT技术 · 发布时间: 4年前

内容简介:The default export is the hook functionIt is allowed to mix

Introduction

PreSWR is a wrapper over swr that keeps track of your calls and lets you preload its results. It's oriented as a tool to help with Server-Side Rendering, and quite suited for use with Next.js , although its design is environment-agnostic.

$ yarn add preswr
import usePreSWR, { preloaded } from "preswr"
import React from "react"

const FavouritePokemon = preloaded(({ name }) => {
  const { data } = usePreSWR(`https://pokeapi.co/api/v2/pokemon/${name}/`)
  return (
    <p>
      My favourite Pokémon is ${data.name}, of type ${data.types[0].type.name}
    </p>
  )
})

export default FavouritePokemon.Component

export const getStaticProps = async ({ params }) =>
  await FavouritePokemon.preloadData({ name: params.name })

Usage

usePreSWR

The default export is the hook function usePreSWR , which wraps useSWR with the preloading smarts. It has the same arguments.

It is allowed to mix usePreSWR calls and useSWR calls freely, but only the usePreSWR will be preloaded.

PreSWRConfig

usePreSWR can't read the config from the <SWRConfig> provider. We export a <PreSWRConfig> that works for both.

preloaded

It's a function which takes a React component, and an optional options object, and returns an object with two properties.

preloaded(
  MyComponent,
  /* this object, and all its properties, are optional */
  {
    /**
     * PreSWR analyzes the tree multiple times to make sure that all
     * conditional rendering is taken care of. You can configure how many times
     * this can happen, or set `false` to disable deep analyzing.
     */
    maxDepth: 20,
  }
)
  • Component is your original component, but it takes an extra prop with the preloaded data. You need to use this component in place of the original one to take advantage of the preloading.

  • preloadData() takes the props you'd call your component with, preloads the data which it would request, and returns the props with an extra one which holds the preloaded data.

Limitations

  • It only works with ReactDOM, so no support for React Native (yet?)
  • Make sure you follow the Rules of Hooks
  • When preloadData is called, your React component is rendered in the background (maybe multiple times!), which might invoke side-effects if your code uses them.
  • Your fetcher functions must work in the context you're using preloadData() . You can provide an initialData config key in the call to bypass calling them.
  • Your key s (or the value returned from key functions) must be able to be serialized to JSON (that means numbers, strings, null, arrays, and plain objects).

TypeScript

This package is written in TypeScript, and it exports a number of helpful types .

An especially useful type is PreloaderProps<...> , which returns the type of the props of the wrapped component, including the extra internal prop which holds the preloaded data.

import { GetStaticProps } from "next"
import { preloaded, PreloaderProps } from "preswr"
import FooComponent from "./foo"

const PreloadedFoo = preloaded(FooComponent)

export default PreloadedFoo.Component

export const getStaticProps: GetStaticProps<PreloaderProps<
  typeof PreloadedFoo
>> = async ({ params }) =>
  await FavouritePokemon.preloadData({ name: params.name })

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

移动风暴

移动风暴

[美]弗雷德·沃格尔斯坦 / 朱邦芊 / 中信出版社 / 2014-1-1 / 39

也许,除了伟大的乔布斯,每一位奋力改变世界的硅谷英雄,都值得我们肃然起敬。苹果与谷歌十年博弈,关于这场移动平台战争的报道早已铺天盖地,而这是第一次,我们能听到幕后工程师的真实声音。两大科技巨人用智能手机和平板电脑颠覆了电脑产业。它们位处变革的中心,凭借各自的经营哲学、魅力领袖和商业敏感度,把竞争变成了残酷对决。商业记者沃格尔斯坦报道这场对抗已逾十载,在《移动风暴》中,他带领我们来到一间间办公室和会......一起来看看 《移动风暴》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

MD5 加密
MD5 加密

MD5 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具