当前位置: 首页 > wzjs >正文

关于企业网站建设的市场比质比价调查报告网站团队组成

关于企业网站建设的市场比质比价调查报告,网站团队组成,制作ppt模板的软件,做网站賺钱简介 SWR(stale-while-revalidate)是由 Vercel 团队推出的 React Hooks 数据请求库,专注于高效的数据获取、缓存和自动更新。它基于 HTTP 缓存的“陈旧-再验证”策略,能够让页面始终显示最新的数据,同时保证极致的性能…

简介

SWR(stale-while-revalidate)是由 Vercel 团队推出的 React Hooks 数据请求库,专注于高效的数据获取、缓存和自动更新。它基于 HTTP 缓存的“陈旧-再验证”策略,能够让页面始终显示最新的数据,同时保证极致的性能和用户体验。

SWR 的核心优势包括:

  • 自动缓存和请求去重:相同请求只会发送一次,数据自动缓存,避免重复请求。
  • 实时数据更新:支持自动轮询、焦点重新获取、网络恢复自动刷新等机制,保证数据新鲜。
  • 灵活的错误重试和加载状态管理:内置错误重试、加载指示等,开发体验极佳。
  • 与后端无关:支持 RESTful、GraphQL、数据库等多种数据源,fetcher 可自定义。

安装 SWR

npm install swr

基本用法

SWR 的核心是 useSWR Hook。最简单的用法如下:

import useSWR from "swr";const fetcher = (url) => fetch(url).then((res) => res.json());export default function SwrDemo() {const { data, error, isLoading, mutate } = useSWR("https://api.github.com/users/octocat",fetcher);if (isLoading) return <div className="container">加载中...</div>;if (error) return <div className="container">加载失败: {error.message}</div>;return (<div className="container" style={{ padding: 24 }}><h2>GitHub 用户信息(octocat)</h2><imgsrc={data.avatar_url}alt="avatar"width={80}style={{ borderRadius: 8 }}/><p><strong>用户名:</strong> {data.login}</p><p><strong>简介:</strong> {data.bio}</p><p><strong>主页:</strong>{" "}<a href={data.html_url} target="_blank" rel="noopener noreferrer">{data.html_url}</a></p><button onClick={() => mutate()}>刷新</button></div>);
}

手动触发请求(mutate)

有时候你需要手动刷新数据,比如用户更新后:

import React from "react";
import useSWR from "swr";const fetcher = (url) => fetch(url).then((res) => res.json());export default function SwrDemo() {// 固定 key,不自动请求const { data, error, isLoading, mutate } = useSWR("octocat", {fetcher: () => {}, // 默认不请求revalidateOnMount: false, // 挂载时不自动请求});// 手动触发 mutateconst handleFetch = async () => {await mutate(() => fetcher("https://api.github.com/users/octocat"), {revalidate: false,});};return (<div className="container" style={{ padding: 24 }}><h2>SWR mutate 手动触发请求示例</h2><buttononClick={handleFetch}disabled={isLoading}style={{padding: "8px 16px",marginBottom: 16,backgroundColor: isLoading ? "#ccc" : "#1890ff",color: "white",border: "none",borderRadius: 4,cursor: isLoading ? "not-allowed" : "pointer",}}>{isLoading ? "请求中..." : "手动获取 GitHub 用户信息"}</button>{error && (<div style={{ color: "red", marginBottom: 16 }}>请求失败: {error.message}</div>)}{data && (<divstyle={{padding: 16,border: "1px solid #d9d9d9",borderRadius: 4,backgroundColor: "#fafafa",}}><h3>用户信息</h3><imgsrc={data.avatar_url}alt="avatar"width={80}style={{ borderRadius: 8, marginBottom: 8 }}/><p><strong>用户名:</strong> {data.login}</p><p><strong>简介:</strong> {data.bio || "暂无简介"}</p><p><strong>主页:</strong>{" "}<a href={data.html_url} target="_blank" rel="noopener noreferrer">{data.html_url}</a></p></div>)}</div>);
}

自动重新请求

import useSWR from "swr";const fetcher = (url) => fetch(url).then((res) => res.json());export default function Demo() {const { data, error, isLoading } = useSWR("https://api.github.com/users/octocat",fetcher,{refreshInterval: 3000, // 每3秒自动重新请求一次revalidateOnFocus: false, // 切换标签页时不自动请求});return (<div className="container" style={{ padding: 24 }}><h2>SWR 自动重新请求(轮询)示例</h2><p style={{ color: "#888", fontSize: 12 }}>每3秒自动刷新一次数据</p>{isLoading && <div>加载中...</div>}{error && <div style={{ color: "red" }}>请求失败: {error.message}</div>}{data && (<divstyle={{padding: 16,border: "1px solid #d9d9d9",borderRadius: 4,background: "#fafafa",}}><h3>用户信息</h3><imgsrc={data.avatar_url}alt="avatar"width={80}style={{ borderRadius: 8, marginBottom: 8 }}/><p><strong>用户名:</strong> {data.login}</p><p><strong>简介:</strong> {data.bio || "暂无简介"}</p><p><strong>主页:</strong>{" "}<a href={data.html_url} target="_blank" rel="noopener noreferrer">{data.html_url}</a></p></div>)}</div>);
}

分页 useSWRInfinite

import React from "react";
import useSWRInfinite from "swr/infinite";const fetcher = (url) => fetch(url).then((res) => res.json());// 获取每一页的 key
const getKey = (pageIndex, previousPageData) => {// 没有更多数据时if (previousPageData && previousPageData.length === 0) return null;// GitHub 用户分页API(每页5个)return `https://api.github.com/users?since=${pageIndex * 5}&per_page=5`;
};export default function Demo() {const { data, error, isLoading, size, setSize, isValidating } =useSWRInfinite(getKey, fetcher);// 合并所有页的数据const users = data ? [].concat(...data) : [];const isEmpty = data?.[0]?.length === 0;const isReachingEnd = isEmpty || (data && data[data.length - 1]?.length < 5);return (<div className="container" style={{ padding: 24 }}><h2>SWR useSWRInfinite 分页示例</h2><p style={{ color: "#888", fontSize: 12 }}>每页5个用户,点击加载更多</p>{isLoading && <div>加载中...</div>}{error && <div style={{ color: "red" }}>请求失败: {error.message}</div>}<ul style={{ padding: 0, listStyle: "none" }}>{users.map((user) => (<likey={user.id}style={{ display: "flex", alignItems: "center", marginBottom: 12 }}><imgsrc={user.avatar_url}alt={user.login}width={36}style={{ borderRadius: 18, marginRight: 12 }}/><a href={user.html_url} target="_blank" rel="noopener noreferrer">{user.login}</a></li>))}</ul><buttononClick={() => setSize(size + 1)}disabled={isLoading || isValidating || isReachingEnd}style={{padding: "8px 16px",backgroundColor:isLoading || isValidating || isReachingEnd ? "#ccc" : "#1890ff",color: "white",border: "none",borderRadius: 4,cursor:isLoading || isValidating || isReachingEnd? "not-allowed": "pointer",}}>{isReachingEnd ? "没有更多了" : isValidating ? "加载中..." : "加载更多"}</button></div>);
}

配置选项

SWR 支持很多配置,比如自动刷新、错误重试等:

const { data, error } = useSWR("/api/user", fetcher, {refreshInterval: 5000, // 每 5 秒自动刷新revalidateOnFocus: true, // 页面聚焦时自动刷新shouldRetryOnError: false, // 出错时不自动重试
});

更多配置参数

配置项类型默认值说明
refreshIntervalnumber0自动刷新间隔(毫秒),0 表示不自动刷新
revalidateOnFocusbooleantrue页面聚焦时自动重新请求
shouldRetryOnErrorbooleantrue请求出错时是否自动重试
dedupingIntervalnumber2000去重请求的时间间隔(毫秒)
errorRetryCountnumber5出错时重试的最大次数
errorRetryIntervalnumber5000出错重试的时间间隔(毫秒)
fallbackDataanyundefined初始数据,未请求到数据时使用
suspensebooleanfalse是否启用 React Suspense
onSuccessfunction请求成功时的回调
onErrorfunction请求失败时的回调

与数据库的关系

SWR 只是前端数据请求和缓存工具,实际数据库操作由后端 API 完成。SWR 负责高效请求、缓存和自动刷新,提升用户体验。


总结

  • SWR 适合前端高效请求和缓存数据
  • 支持自动刷新、错误重试、手动刷新等高级用法
  • 结合后端 API,可实现与数据库的数据交互

如需更复杂的用法(如分页、依赖请求等)

 React Hooks 数据请求库——SWR使用详解 - 高质量源码分享平台-免费下载各类网站源码与模板及前沿动态资讯


文章转载自:

http://EldbDN7B.smnxr.cn
http://ifJ8qQse.smnxr.cn
http://kCkOfosx.smnxr.cn
http://adeZfl9x.smnxr.cn
http://VyCFzxs4.smnxr.cn
http://3Dny3Bk8.smnxr.cn
http://oYAwm1km.smnxr.cn
http://GDTqQ4jT.smnxr.cn
http://RHoBWGtC.smnxr.cn
http://ZxaKMXst.smnxr.cn
http://4W2XCWK9.smnxr.cn
http://krmpvgE4.smnxr.cn
http://0eZBiFSC.smnxr.cn
http://vt8G5exX.smnxr.cn
http://DSYV02Pf.smnxr.cn
http://Bo3zLH9W.smnxr.cn
http://Aq20Dhr2.smnxr.cn
http://MBFfkl85.smnxr.cn
http://q3T9Q43d.smnxr.cn
http://TjXFFXhZ.smnxr.cn
http://FdpiIs3S.smnxr.cn
http://IYfjQSIS.smnxr.cn
http://Rem77VGk.smnxr.cn
http://b02fVJjw.smnxr.cn
http://BbSvoisx.smnxr.cn
http://uy1g9FuH.smnxr.cn
http://JrbZoXy2.smnxr.cn
http://5xdosYQ6.smnxr.cn
http://wxBnNCuX.smnxr.cn
http://X7pVZeXu.smnxr.cn
http://www.dtcms.com/wzjs/686697.html

相关文章:

  • 网站展示重点全网营销的概念和特点
  • 网站案例分析湖南做照片的网站有哪些
  • 天猫网站设计特点青岛注册公司流程
  • 给人做网站的公司阿里云虚拟主机做2个网站吗
  • 做网站设计师工资多少备案网站
  • 河南广宇建设集团有限公司网站网站建设地图怎么设置
  • 北京中御建设公司网站阿里巴巴官网下载安装
  • 把里面的dede和plugins这2个文件夹覆盖到你的网站根目录做ppt模板下载网站
  • 160 国际英文网站如何做自己的淘宝优惠券网站
  • 手机网站建设cz35网站开发工程师是什么内容
  • 网站联动是什么意思北京国互网网站建设公司
  • 申请域名网站价格个人网站 推荐
  • 广西自治区集约化网站建设要求苏州园区公积金管理中心官网
  • 合肥大型网站seo是什么的简称
  • 修改网站版权怎么创建wordpress站点
  • 北京网站建设怎么样网络营销推广方式案例分析
  • 专业美工设计网站建设wordpress打开文件
  • 网站建设需求调研过程建设网站的基本知识
  • 铭讯网站建设wordpress 头部 微博
  • 丹江口市建设局网站网站维护工单
  • 拱墅网站建设网站通知做文献的格式
  • 唐山高端品牌网站建设自己网站怎么做优化
  • 想做机械加工和橡胶生意怎么做网站福州男同性做基网站
  • 邯郸做网站费用手机设计房子的软件
  • 明星用什么软件做视频网站添加网站绑定主机名
  • 沈阳企业模板建站wordpress商务版
  • 建设银行网站怎么不可登入电商网站seo方案
  • 点击进入官方网站电子商务网站建设及推广
  • 网站推广怎么做比较好爱站网络科技有限公司
  • 怎么建设网站网页游戏公司注册地址可以是住宅