wait-component
Component for handling async rendering with transient states.
Installation
npm i wait-component
Usage
Wait with render prop
Use the render prop pattern when you need to use the resolved value from the promise.
import { Wait } from 'wait-component';
function App() {
const promise = fetchUser();
return (
<Wait
promise={promise}
transient={{ name: "Loading..." }}
render={(user, pending) => <div>{user.name}</div>}
fallback={null}
/>
);
}Props:
promise: Promise to wait forrender: Function that receives the resolved value (or transient value during fallback) andpendingflagtransient: Optional value to display during loadingfallback: Fallback to render iftransientis undefinedalways: If true, always callsrenderduring fallback
Wait with children
Use the children pattern when you just need to wait for the promise to complete, without using the resolved value.
import { Wait } from 'wait-component';
function App() {
const promise = saveData();
return (
<Wait promise={promise} fallback={<div>Saving...</div>}>
<div>Save completed!</div>
</Wait>
);
}Props:
promise: Promise to wait for completionchildren: Content to render after the promise resolvesfallback: Fallback to render during loading
License
MIT