0.1.7 • Published 5 years ago

remote-data-component v0.1.7

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

remote-data-component

A library to allow for better remote data UI managment.

Learn more: https://medium.com/unpacking-trunk-club/using-redux-and-redux-saga-to-handle-api-calls-18964d234660

Installation

npm install remote-data-component

Usage

RemoteDataT

This type signifies how remote data should be perceived in correlation to this package and it's components and functions

type RemoteDataT<D, E, M = any> =
  | { phase: 'NOT_ASKED', meta?: M }
  | { phase: 'PENDING', meta?: M }
  | { phase: 'SUCCESS', data: D, meta?: M }
  | { phase: 'ERROR', error: ?E, meta?: M }

Learn more about Union Types

Flow Typescript

RemoteDataComponent

This component allows a component to be rendered differently for different phases of remote data calls

PropRequiredTypeDescription
datayes{[key: string]: RemoteDataT<*, *>}Object containing API data in the RemoteDataT type
renderNotAsked() => React.NodeRenders component when not API calls have been started
renderPending() => React.NodeRenders when at least ONE call is pending
renderSuccess() => React.NodeRenders when ALL calls are successful
renderError() => React.NodeRenders when at leaset ONE call has an error
import React from 'react'
import RemoteDataComponent from 'RemoteDataComponent'

class MyComponent extends React.Component {
  render() {
    const { MyDataOne, MyDataTwo } = this.props

    return (
      <RemoteDataComponent
        data={{ MyDataOne, MyDataTwo }}
        renderNotAsked={() => <Empty />}
        renderPending={() => <Loading />}
        renderError={() => <Error />}
        renderSuccess={data => (
          <Content one={ data.MyDataOne.data } two={ data.MyDataTwo.data } />
        )}
      />
    )
  }
}

onStateTransition

This utils function is meant to be called when some actions should be taken when a call transitions (eg. PENDING -> SUCCESS or PENDING -> ERROR)

ParameterRequiredTypeDescription
prevStateyesRemoteDataT<*, *>Previous API call object
nextStateyesRemoteDataT<*, *>Next API call object
onSuccess() => voidCalled when state transistion from non-Success to Success
onError() => voidCalled when state transistion from non-Error to Error
onPending() => voidCalled when state transistion from non-Pending to Pending
onInit() => voidCalled when state transistion from non-Init to Init
import React from 'react'
import { onStateTransition } from 'RemoteDataComponent'

class MyComponent extends React.Component {
  componentDidUpdate(prevProps) {
    onStateTransition({
      prevState: prevProps.MyData,
      nextState: this.props.MyData,
      onSuccess: () => console.log('Success!')
      onError: () => console.warn('Error!')
    })
  }
}

remoteDataReducer

This utils function is meant to create a reducer function that facilitates the RemoteDataT API call concept

ParameterRequiredTypeDescription
prefixyesstringPrefix string to prefix action types (eg. MyData/PENDING)
enhanceryes(state: StateT, action: ActionT) => StateTCustom reducer that can be called to account for other action types
import { remoteDataReducer } from 'remote-data-component'

export default remoteDataReducer('MyData')
0.2.0-alpha.6

5 years ago

0.2.0-alpha.5

5 years ago

0.2.0-alpha.4

5 years ago

0.2.0-alpha.3

5 years ago

0.2.0-alpha.2

5 years ago

0.2.0-alpha.0

5 years ago

0.2.0-alpha.1

5 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago