# r3shaper-react

> R3shaper HOC, hook and component for React

Latest version **0.1.1** (published 2019-12-31) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install r3shaper-react
pnpm add r3shaper-react
yarn add r3shaper-react
bun add r3shaper-react
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2019-12-31 |
| First published | 2019-11-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 10.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | coltor-apps |
| Maintainers | coltorapps |
| Keywords | r3shaper, react, API, Rest, HOC, hook, r3shaper component |

## Links

- npm: https://www.npmjs.com/package/r3shaper-react
- Repository: https://github.com/coltor-apps/r3shaper-react
- Homepage: https://github.com/coltor-apps/r3shaper-react#readme
- Issues: https://github.com/coltor-apps/r3shaper-react/issues
- npm.io page: https://npm.io/package/r3shaper-react

## Dependencies (2)

- [p-debounce](https://npm.io/package/p-debounce.md) ^2.1.0
- [p-throttle](https://npm.io/package/p-throttle.md) ^3.1.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.1.1 (latest) — 2019-12-31
- 0.1.0 — 2019-12-04
- 0.0.2 — 2019-11-27
- 0.0.1 — 2019-11-27

## README

<p align="center">
✨R3shaper Hook, Component and HOC for React✨
</p>

# R3shaper for React

## Installation

Install the package using npm

```bash
npm install r3shaper r3shaper-react
```

or if you're using yarn

```bash
yarn add r3shaper r3shaper-react
```

### Pre-configurations

In order to use r3shaper-react components, we'll need to have configured an instance of [r3shaper](http://github.com/coltor-apps/r3shaper) client, so let's assume we have following resource:

```js
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:

```js
import { useR3shaper } from 'r3shaper-react';
/* ... */
const { response, error, loading, dispatch } = useR3shaper(resource, options);
```

| Argument           | Type     | Default | Description                             |
| ------------------ | -------- | ------- | --------------------------------------- |
| `resource`         | Function | -       | R3shaper resource method                |
| `options.debounce` | Number   | 0       | Debounce time (ms)                      |
| `options.throttle` | Number   | 0       | Throttle time (ms)                      |
| `options.manual`   | Boolean  | false   | Shall the request be triggered manually |

<br/>
<p id="hook_properties_docs">

| Property   | Type     | Description                                                                                                          |
| ---------- | -------- | -------------------------------------------------------------------------------------------------------------------- |
| `response` | Object   | Response object                                                                                                      |
| `error`    | Object   | Error object                                                                                                         |
| `loading`  | Boolean  | Indicates if the request was finished or not                                                                         |
| `dispatch` | Function | Function invoked to dispatch the request. Accepts exactly the same arguments as an ordinary r3shaper resource method |

</p>



## Component

```jsx
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

| Prop       | Type     | Default | Description                                                                                                               |
| ---------- | -------- | ------- | ------------------------------------------------------------------------------------------------------------------------- |
| `resource` | Function | -       | R3shaper resource method                                                                                                  |
| `debounce` | Number   | 0       | Debounce time (ms)                                                                                                        |
| `throttle` | Number   | 0       | Throttle time (ms)                                                                                                        |
| `manual`   | Boolean  | false   | Shall the request be triggered manually                                                                                   |
| `children` | Function | -       | React component that will receive following props: `loading`, `response`, `error` and `dispatch` ([see hook documentation](#hook_properties_docs)) |

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




## Higher-Order Component

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

### withR3shaper arguments

| Argument            | Type     | Default | Description                             |
| ------------------- | -------- | ------- | --------------------------------------- |
| `OriginalComponent` | Function | -       | React component                         |
| `resource`          | Function | -       | R3shaper resource method                |
| `options.debounce`  | Number   | 0       | Debounce time (ms)                      |
| `options.throttle`  | Number   | 0       | Throttle time (ms)                      |
| `options.manual`    | Boolean  | false   | Shall the request be triggered manually |

<br/>

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

Created with ❤️ by [Sergiu Masurceac](https://twitter.com/masurceac)

---
_Source: https://npm.io/package/r3shaper-react · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
