项目作者: erzhtor

项目描述 :
See examples
高级语言: JavaScript
项目地址: git://github.com/erzhtor/react-async-data.git
创建时间: 2018-09-08T11:37:35Z
项目社区:https://github.com/erzhtor/react-async-data

开源协议:

下载


react-async-data

React Async Data Component

Build Status NPM JavaScript Style Guide

  • Easy fetch and pass data
  • dynamic refetch based on timeout interval
  • custom refetch
  • TypeScript typings

Install

  1. npm install --save react-async-data

or

  1. yarn add react-async-data

Usage

import Component

  1. import React from "react";
  2. import ReactAsyncData from "react-async-data";
  1. <ReactAsyncData
  2. timeout={1000}
  3. fetch={() => {
  4. // fetch your data here
  5. return Promise.resolve({ title: "some cool title" });
  6. }}
  7. >
  8. {({ status, data }) => (
  9. <React.Fragment>
  10. {status === "loading" && "loading"}
  11. {status === "error" && "error"}
  12. {status === "completed" && data && <div>{data.title}</div>}
  13. </React.Fragment>
  14. )}
  15. </ReactAsyncData>

More examples

Props

  • fetch: () => Promise<TData>. Required. Function that returns promise. Any data that promise resolves passed to children as data object.
  • timeout: number. Optional. If passed refetches every given timeout (millis).
  • fetchId: number | string | boolean. Optional. Refetches every time when changed.
  • children: (args: { loading: boolean; error?: any; data?: TData;}) => React.ReactNode. Optional. Children would be called with the specified args params.

License

MIT © Erzhan Torokulov