Skip to content

Latest commit

 

History

History
77 lines (68 loc) · 1.1 KB

request-cache.md

File metadata and controls

77 lines (68 loc) · 1.1 KB

React 一个请求先返回缓存之后刷新数据的解决方案

  • fetch(opts, ({ data: any, isCache: bool }) => void): Promise
this.setState({
  isLoading: true,
});
try {
  await fetch(opts, ({ data, isCache}) => {
    this.setState({
      data,
      isLoading: !isCache,
    });
  });
} catch (error) {
  this.setState({
    error,
    isLoading: false,
  });
}
  • fetch(opts): [{ data: any, isCache: bool }] (for await)
this.setState({
  isLoading: true,
});
try {
  for await ({ data, isCache } of fetch()) {
    this.setState({
      data,
      isLoading: !isCache,
    });
  }
} catch (error) {
  this.setState({
    error,
    isLoading: false,
  });
}
  • fetch(opts): Promise<{data, next: Promise}>
this.setState({
  isLoading: true,
});
try {
  const { data, next } await fetch();
  this.setState({
    data,
  });
  const newData = await next;
  this.setState({
    data: newData,
    isLoading: false,
  });
} catch (error) {
  this.setState({
    error,
    isLoading: false,
  });
}
  • generator

{
  cacheData,
}