use-taro-request 是基于 taro 及 react 开发的主要用于网络请求的 hooks。
npm
npm install use-taro-request --save
yarn
yarn add use-taro-request
使用 taroRequestConfig
在程序入口文件,app.tsx
或者main.ts
等中导入并全局配置
import { taroRequestConfig } from "use-taro-request";
taroRequestConfig({
baseUrl: "https://example.com",
header: {}
// ...
});
import { useTaroRequest } from "use-taro-request";
const { data, loading, error, refetch } = useTaroRequest({ url: "/list" });
通过 manual
属性配置依赖请求,当传入 true
时,将需要自己手动触发 refetch
import { useTaroRequest } from "use-taro-request";
const { data, loading, error, refetch } = useTaroRequest(
{
url: "/list",
params: {
//...
}
},
{ manual: true }
);
Property | Type | Default | Description |
---|---|---|---|
baseUrl |
string |
'' |
基础网络地址 |
header |
object |
{} |
请求头 |
timeout |
number |
0 |
请求 timeout |
Property | Type | Default | Description |
---|---|---|---|
manual |
boolean |
false |
是否手动触发请求 |