0.1.1 • Published 4 years ago

r3shaper-react v0.1.1

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

R3shaper for React

Installation

Install the package using npm

npm install r3shaper r3shaper-react

or if you're using yarn

yarn add r3shaper r3shaper-react

Pre-configurations

In order to use r3shaper-react components, we'll need to have configured an instance of r3shaper client, so let's assume we have following resource:

import apiClient from './';

export const UsersResource = {
  list: apiClient.get('/users'),
  delete: apiClient.delete('/users/{id}')
};

Hook

R3shaper hook accepts 2 arguments and returns an object with 4 properties, as shown in the code snippet below:

import { useR3shaper } from 'r3shaper-react';
/* ... */
const { response, error, loading, dispatch } = useR3shaper(resource, options);
ArgumentTypeDefaultDescription
resourceFunction-R3shaper resource method
options.debounceNumber0Debounce time (ms)
options.throttleNumber0Throttle time (ms)
options.manualBooleanfalseShall the request be triggered manually
PropertyTypeDescription
responseObjectResponse object
errorObjectError object
loadingBooleanIndicates if the request was finished or not
dispatchFunctionFunction invoked to dispatch the request. Accepts exactly the same arguments as an ordinary r3shaper resource method

Component

import { R3shaper } from 'r3shaper-react';
import { UserResource } from './api';

function ExampleComponent() {
  return (
    <R3shaper
      debounce={100}
      throttle={500}
      resource={UserResource.list}
    >
      {({ loading, response, dispatch, error }) => (
          <div>
            {loading && <SomeLoadingIndicator/>}
            {response && <SomeResponseWrapper data={response.data}/>}
            {error && <SomeErrorWrapper error={error}>}
            <button onClick={() => dispatch({queryParams: {page: 2}})}>Fetch</button>
          </div>
      )}
    </R3shaper>
  );
}

R3shaper component Props

PropTypeDefaultDescription
resourceFunction-R3shaper resource method
debounceNumber0Debounce time (ms)
throttleNumber0Throttle time (ms)
manualBooleanfalseShall the request be triggered manually
childrenFunction-React component that will receive following props: loading, response, error and dispatch (see hook documentation)

All other props passed to R3shaper will be passed down to it's children

Higher-Order Component

import { withR3shaper } from 'r3shaper-react';
/* ... */
export default withR3shaper(OriginalComponent, resource, options);

withR3shaper arguments

ArgumentTypeDefaultDescription
OriginalComponentFunction-React component
resourceFunction-R3shaper resource method
options.debounceNumber0Debounce time (ms)
options.throttleNumber0Throttle time (ms)
options.manualBooleanfalseShall the request be triggered manually

withR3shaper will pass down to OriginalComponent 4 new props: loading, response, error and dispatch (see hook documentation)

Created with ❤️ by Sergiu Masurceac