README
react-promise-state
React component for easier promise handling
Install
npm
npm install --save react-promise-state
Importing
ES6 imports:
import ReactPromiseState from "react-promise-state";
Usage
class App extends Component {
render() {
return (
<div>
<ReactPromiseState
run={fetchUsers}
render={({ run, done, pending, error, result }) => (
<div>
<button onClick={run}>Fetch Users</button>
{pending && <span>loading ...</span>}
{error && <span>Received an error while fetching users</span>}
<div>{JSON.stringify(result)}</div>
</div>
)}
/>
</div>
);
}
}
Props
{function} (required) run
Will be called when you call run from the render prop
{function} (required) render
Classic render prop
This function will receive an object with following properties
{function} run
When called will call the promise
{Boolean} done
True if the promise resolved
{Boolean} pending
True if the promise is pending
{Boolean} result
The result of the promise
{Boolean} error
If the promise fails, this will be the error
Contribution
I welcome issues, pull requests and new ideas on https://github.com/MichalSzorad/react-promise-state