Minimal loader with state management capabilities.
Checkout out the demo.
Make sure to add a .babelrc
file:
{
"presets": ["es2015", "stage-0", "react"],
"plugins": ["transform-flow-strip-types", "transform-object-rest-spread"]
}
Uses multiple render props. Loader renders the correct view according to the calculated state.
const fakeFetch = () =>
new Promise((res, rej) => {
setTimeout(
() =>
res([
{ id: 1, name: 'foo' },
{ id: 2, name: 'bar' },
{ id: 3, name: 'baz' }
]),
1000
);
});
const App = () => (
<Loader
fetch={fakeFetch}
renderError={error => (
<div class="error">Something went wrong: {error.message}</div>
)}
renderSuccess={data => <UserItems data={data} />}
renderNotAsked={() => <div className="start">Not Data Loaded</div>}
renderLoading={() => <div className="loader">Loading...</div>}
>
{(View, loadData) => (
<div>
{View}
<button onClick={loadData}>Load Data</button>
</div>
)}
</Loader>
);