All Projects → capaj → React Promise

capaj / React Promise

Licence: mit
a react.js hook for general promise in typescript

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to React Promise

React Native Pdfview
📚 PDF viewer for React Native
Stars: ✭ 198 (+120%)
Mutual labels:  hacktoberfest, react-component
Pokeapi Js Wrapper
PokeAPI browser wrapper, fully async with built-in cache
Stars: ✭ 129 (+43.33%)
Mutual labels:  hacktoberfest, asynchronous
React Sight
Visualization tool for React, with support for Fiber, Router (v4), and Redux
Stars: ✭ 2,716 (+2917.78%)
Mutual labels:  hacktoberfest, react-component
Bastion
Highly-available Distributed Fault-tolerant Runtime
Stars: ✭ 2,333 (+2492.22%)
Mutual labels:  hacktoberfest, asynchronous
Pigeon Maps
ReactJS Maps without external dependencies
Stars: ✭ 3,198 (+3453.33%)
Mutual labels:  hacktoberfest, react-component
Sample Plugin
Example plugin, generated using `wp scaffold plugin sample-plugin`
Stars: ✭ 89 (-1.11%)
Mutual labels:  hacktoberfest
Apac Conferences
A community contributed consolidated list of InfoSec meetups in the Asia Pacific region.
Stars: ✭ 90 (+0%)
Mutual labels:  hacktoberfest
Surf N Perf
Micro-library for gathering web page performance data
Stars: ✭ 89 (-1.11%)
Mutual labels:  hacktoberfest
Github Contributions
A tool that generates a repository which being pushed into your GitHub account creates a nice contributions calendar.
Stars: ✭ 1,295 (+1338.89%)
Mutual labels:  hacktoberfest
Powershell
PowerShell functions and scripts (Azure, Active Directory, SCCM, SCSM, Exchange, O365, ...)
Stars: ✭ 1,302 (+1346.67%)
Mutual labels:  hacktoberfest
Digitalocean Js
JavaScript library for the DigitalOcean API
Stars: ✭ 90 (+0%)
Mutual labels:  hacktoberfest
Hacktoberfest
Find more projects at https://hacktoberfest.digitalocean.com/#projects
Stars: ✭ 90 (+0%)
Mutual labels:  hacktoberfest
Silverstripe Gridfieldextensions
A collection of useful grid field components.
Stars: ✭ 89 (-1.11%)
Mutual labels:  hacktoberfest
Protobuf Nim
Protobuf implementation in pure Nim that leverages the power of the macro system to not depend on any external tools
Stars: ✭ 90 (+0%)
Mutual labels:  hacktoberfest
Silverstripe Fluent
Multi-language translate module for Silverstripe, without having to manage separate site trees.
Stars: ✭ 89 (-1.11%)
Mutual labels:  hacktoberfest
Rebar3
Erlang build tool that makes it easy to compile and test Erlang applications and releases.
Stars: ✭ 1,295 (+1338.89%)
Mutual labels:  hacktoberfest
Vuex Map Fields
Enable two-way data binding for form fields saved in a Vuex store
Stars: ✭ 1,294 (+1337.78%)
Mutual labels:  hacktoberfest
Cloud haiku
Community-made poetry about infrastructure
Stars: ✭ 90 (+0%)
Mutual labels:  hacktoberfest
Docker Ansible Playbook
Docker Image of Ansible for executing ansible-playbook command against an externally mounted set of Ansible playbooks
Stars: ✭ 90 (+0%)
Mutual labels:  hacktoberfest
Maskable
Create and preview maskable icons in the browser!
Stars: ✭ 90 (+0%)
Mutual labels:  hacktoberfest

react-promise

NPM badge

a react.js hook for general promise written in typescript. Let's consider a trivial example: you have a promise such as this

let prom = new Promise(function(resolve, reject) {
  setTimeout(function() {
    resolve('a value')
  }, 100)
})

and you want to make a component, which renders out in it's body 'a value'. Without react-promise, such component may look like this:

class ExampleWithoutAsync extends React.Component { // you can't use stateless component because you need a state
  constructor () {
    super()
    this.state = {}
  }
  componentDidMount() {
    prom.then((value) => {
      this.setState({val: value})
    })
  }
  render () {
    if (!this.state.val) return null
    return <div>{this.state.val}</div>
  }

// or you could use a combination of useEffect and useState hook, which is basically the implementation of this small library

and with react-promise:

import usePromise from 'react-promise';

const ExampleWithAsync = (props) => {
  const {value, loading} = usePromise<string>(prom)
  if (loading) return null
  return <div>{value}</div>}
}

API

The only argument can be a promise or a promise resolving thunk:

usePromise<T>(
  promiseOrFn: (() => Promise<T>) | Promise<T>
)

it might be desirable to let the usePromise call the promise returnig function, because you often don't want to do that inside the render of your functional component.

Full state object interface returned by the hook looks like:

{
  loading: boolean
  error: Error | null
  value: T | undefined // where T is your shape of the resolved data you expect obviously
}

install

With npm:

npm i react-promise

For version 2 docs, refer to the old readme.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].