1.1.0 • Published 5 years ago

@naveteam/dispatcher v1.1.0

Weekly downloads
14
License
ISC
Repository
github
Last release
5 years ago

Dispatcher React Component

Install

React version below 16.8:

yarn add @naveteam/dispatcher@1.0

React version above 16.8:

yarn add @naveteam/dispatcher

Usage

Without hooks

import Dispatcher from '@naveteam/dispatcher'

const MyComponent = () => (
  <Dispatcher dispatch={() => fetch('https://jsonplaceholder.typicode.com/todos/1')}>
    {({isLoading, content, hasError, reload}) => (
    !isLoading
        ? <div>
            {JSON.stringify(content)}
        </div>
        : <span>loading...</span>
    )}
  </Dispatcher>
)

With hooks

import { useDispatcher } from '@naveteam/dispatcher'

const MyComponent = () => {
    const [isLoading, error, content, reload] = useDispatcher(() => fetch('https://jsonplaceholder.typicode.com/todos/1'))

    if (isLoading) {
        return (
            <span>loading...</span>
        )
    }

    if (error) {
        <span>{JSON.stringify(error)}</span>
    }

    return (
        <div>
            <p>{JSON.stringify(content)}</p>
            <button onClick={reload}>reload</button>
        </div>
    )
}

Component Props

PropvalueExampleRequired
dispatchfunction that returns a promise() => myPromise()yes
shouldUpdateComparison of props to trigger shouldComponentUpdate<Dispatcher lastId={'0'} shouldUpdate={prevProps => prevProps.lastId !== lastId}>{...}</Dispatcher>no
errorCallbackfunction triggered when an request error occurserror => console.log(error)no

Render Props

PropExplanationDefault Value
isLoadingBoolean indicating request has finishedtrue
contentvalue of finished promisenull
hasErrorboolean indicating if request failedfalse
reloadcalls dispatch function

Contribute

Run yarn dev

Edit index.js and App.js inside src folder

1.1.0

5 years ago

1.0.0

5 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

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