首页 > 其他分享 >[React] SWR for data fetching

[React] SWR for data fetching

时间:2022-08-26 18:22:09浏览次数:68  
标签:return fetching listings React import SWR data

Docs

The name “SWR” is derived from stale-while-revalidate, a HTTP cache invalidation strategy popularized by HTTP RFC 5861. SWR is a strategy to first return the data from cache (stale), then send the fetch request (revalidate), and finally come with the up-to-date data.

Example

import React from "react";
import useSWR from "swr";
import { LoadingListings, Listing, ListingsGrid } from "../components";

function Listings(props) {
  const {
    data: listings,
    loading,
    error,
  } = useSWR("https://my.cms.com/listings", (url) =>
    fetch(url).then((r) => r.json())
  );

  if (loading) {
    return <LoadingListings />;
  }

  return (
    <ListingsGrid>
      {listings.map((listing) => (
        <Listing listing={listing} />
      ))}
    </ListingsGrid>
  );
}

 

标签:return,fetching,listings,React,import,SWR,data
From: https://www.cnblogs.com/Answer1215/p/16628567.html

相关文章

  • react中CodeMirror (代码编辑器)
    前言:实现一个在react项目中页面展示代码编辑器的效果。codemirror:使用JavaScript为浏览器实现的多功能文本编辑器。codemirror作用:专门用于编辑代码,并带有实现......
  • React报错之You provided a `checked` prop to a form field
    正文从这开始~总览当我们在多选框上设置了checked属性,却没有onChange处理函数时,会产生"Youprovideda checked proptoaformfieldwithoutan onChange handle......
  • 记录一下react遇到的初始化异步赋值问题
    组件加载时发送接口请求获取数据,在根据收集到的数据的某一项值在进行请求获取相对应的值,实现联动效果1useEffect(()=>{2//getQuestionDetail({id:'61a78f5......
  • react 二级路由嵌套
    嵌套路由之后,静态文静路径错误,更改webpack 打包output输出根目录,publicPath:'/',二级路由刷新之后白屏,在首页模板文件中路径前加  /,   ......
  • localStorge在react中的使用
    1.什么时候用,在哪里用刚获取数据的时候,进行设置,localStorge.setItem(key,value);因为localStorge是用来作为缓存的,且有一定的延时,尤其是在本页面设置本页面使用时,所以,依然......
  • npm+react linux 开荒
    安装npmyuminstallnodejs.x86_64yuminstallnpm.x86_64 更新GCC版本(参考链接:https://blog.csdn.net/qq_39715000/article/details/120703444)升级到gcc7.3yum-y......
  • react实战系列 —— React 中的表单和路由的原理
    其他章节请看:react实战系列React中的表单和路由的原理React中的表单是否简单好用,受控组件和非受控是指什么?React中的路由原理是什么,如何更好的理解React应用的......
  • react+antd upload实现图片宽高、视频宽高尺寸校验
    图片宽高校验方法://上传图片尺寸限制constcheckIconWH=(file:any)=>{returnnewPromise<void>(function(resolve,reject){constfileReader......
  • reactive函数
    <template><div><h1>vue3</h1><span>{{info.name}}-{{info.age}}</span><button@click="infobtn">修改info</button></div></template><scrip......
  • React报错之React hook 'useState' cannot be called in a class component
    正文从这开始~总览当我们尝试在类组件中使用useState钩子时,会产生"Reacthook'useState'cannotbecalledinaclasscomponent"错误。为了解决该错误,请将类组件转换......