react-read
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

react-read

Utility that enables Suspense for Data Fetching.

Usage

Install

yarn add react react-dom react-read
# or with NPM 
npm install react react-dom react-read

Readable.create(object) => readable

If object is a promise, calling readable.read() in your render function will suspend rendering until the promise is resolved. The same is true for readable.value.

https://codesandbox.io/s/snowy-framework-vuvp8

import React from 'react';
import ReactDOM from 'react-dom';
import { Readable } from 'react-read';
import { fetchUser } from './api';
 
const readable = Readable.create(fetchUser(1));
 
function App(props) {
  const user = readable.read(); // or readable.value
  return <h1>Hello {user.name}!</h1>;
}
 
function AppLoading() {
  return <h1>Loading...</h1>;
}
 
ReactDOM.render(
  <React.Suspense fallback={<AppLoading />}>
    <App userId={1} />
  </React.Suspense>,
  document.getElementById('root'),
);

read(promise) => value

Read a promise object. Same as Readable.create(promise).read().

Readme

Keywords

none

Package Sidebar

Install

npm i react-read

Weekly Downloads

11

Version

2.0.0

License

MIT

Unpacked Size

24.7 kB

Total Files

42

Last publish

Collaborators

  • jomaxx