1.0.0 • Published 5 years ago

react-promise-hoc v1.0.0

Weekly downloads
1
License
MIT
Repository
-
Last release
5 years ago

React Promise HOC

React HOC for easy promise handling.

Installation

yarn add react-promise-hoc or npm i react-promise-hoc

Usage

import React, { SFC } from 'react'
import promiseHoc, { InjectedPromiseHocProps } from 'react-promise-hoc'

const Post: SFC<InjectedPromiseHocProps> = ({ isLoading, post, refetch }) =>
  isLoading
    ? <p>Loading...</p>
    : (
      <div>
        <h1>{post.title}</h1>
        <p>{post.content}</p>
        <button onClick={refetch}>Refetch</button>
      </div>
    )

export default promiseHoc((props) => ({
  post: axios.get(`https://jsonplaceholder.typicode.com/posts/${props.id}`)
}), (err, data, props) => { /* gets invoked when promise ir rejected / resolved */ })(Post)

// also possible to provide an array
// notice: this will provide a prop called data which will be an array of the resolved data
export default promiseHoc((props) => ([
  axios.get(`https://jsonplaceholder.typicode.com/posts/${props.id}`)
]))(Post)

Documentation

Injected props to child component

PropsTypeDescription
isLoadingbooleanSet to true when promise(s) are pending and false when done.
dataarrayIf an array is passed this will be set to the resolved data as an array
errobjectThe error object if one ore more promises got rejected.
refetchfunctionWhen called the promises will reinvoke
1.0.0

5 years ago

0.0.17

6 years ago

0.0.16

6 years ago

0.0.15

6 years ago

0.0.14

6 years ago

0.0.13

6 years ago

0.0.12

6 years ago

0.0.11

6 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.6

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago