|
1 | | -[![Build status][travisci-image]][travisci-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Quality Gate][quality-gate-image]][quality-gate-url] |
| 1 | +[![Build status][travisci-image]][travisci-url] |
2 | 2 |
|
3 | 3 | [![NPM dependencies][npm-dependencies-image]][npm-dependencies-url] [](https://renovatebot.com) [![Last commit][last-commit-image]][last-commit-url] [![Last release][release-image]][release-url] |
4 | 4 |
|
5 | 5 | [![NPM downloads][npm-downloads-image]][npm-downloads-url] [![License][license-image]][license-url] |
6 | 6 |
|
7 | | -# React bindings for @data-provider |
| 7 | +# React bindings for [@data-provider][data-provider] |
8 | 8 |
|
9 | | -> Set of hooks and HOCs for binding @data-provider to React components |
| 9 | +> Set of hooks and HOCs for binding [@data-provider][data-provider] to React components |
| 10 | +
|
| 11 | +## Installation |
| 12 | + |
| 13 | +```bash |
| 14 | +npm i --save @data-provider/react |
| 15 | +``` |
| 16 | + |
| 17 | +## Hooks |
| 18 | + |
| 19 | +### `useDataProvider(provider, [equalityFn])` |
| 20 | + |
| 21 | +Triggers the provider `read` method and gives you the `data`, `loading` and `error` properties from the state of the provider or selector. When the provider cache is cleaned, it automatically triggers `read` again. |
| 22 | + |
| 23 | +Use this hook only when you need all mentioned properties, because your component will be re-rendered any time one of them changes. If you only need one or two properties, better use one of the hooks described bellow. |
| 24 | + |
| 25 | +#### Arguments |
| 26 | + |
| 27 | +* `provider` _(Object)_: [Data Provider][data-provider] provider or selector instance. |
| 28 | +* `equalityFn` _(Function)_: Function used to determine if returned value is different from the previous one, which will produce a re-render of the component. Read [React-redux hooks docs][react-redux-hooks] for further info. |
| 29 | + |
| 30 | +#### Returns |
| 31 | + |
| 32 | +* _(Array)_ - Array containing `data`, `loading` and `error` properties, in that order. |
| 33 | + |
| 34 | +#### Example |
| 35 | + |
| 36 | +```jsx |
| 37 | +import { useDataProvider } from "@data-provider/react"; |
| 38 | + |
| 39 | +import { books } from "../data/books"; |
| 40 | + |
| 41 | +const BooksList = () => { |
| 42 | + const [data, loading, error] = useDataProvider(books); |
| 43 | + // Do your stuff here |
| 44 | +}; |
| 45 | +``` |
| 46 | + |
| 47 | +### `useData(provider, [equalityFn])` |
| 48 | + |
| 49 | +Triggers `read` and gives you only the `data` property from the state of the provider or selector. When the provider cache is cleaned, it automatically triggers `read` again. |
| 50 | + |
| 51 | +Arguments are the same than described for the [`useDataProvider` hook](#usedataproviderprovider-equalityfn). |
| 52 | + |
| 53 | +#### Returns |
| 54 | + |
| 55 | +* _(Any)_ - Value of the `data` property from the provider state. |
| 56 | + |
| 57 | + |
| 58 | +#### Example |
| 59 | + |
| 60 | +```jsx |
| 61 | +import { useData } from "@data-provider/react"; |
| 62 | + |
| 63 | +import { books } from "../data/books"; |
| 64 | + |
| 65 | +const BooksList = () => { |
| 66 | + const data = useData(books); |
| 67 | + // Do your stuff here |
| 68 | +}; |
| 69 | +``` |
| 70 | + |
| 71 | +### `useLoading(provider)` |
| 72 | + |
| 73 | +Triggers `read` and gives you only the `loading` property from the state of the provider or selector. When the provider cache is cleaned, it automatically triggers `read` again. |
| 74 | + |
| 75 | +#### Arguments |
| 76 | + |
| 77 | +* `provider` _(Object)_: [Data Provider][data-provider] provider or selector instance. |
| 78 | + |
| 79 | +#### Returns |
| 80 | + |
| 81 | +* _(Boolean)_ - Value of the `loading` property from the provider state. |
| 82 | + |
| 83 | +#### Example |
| 84 | + |
| 85 | +```jsx |
| 86 | +import { useLoading } from "@data-provider/react"; |
| 87 | + |
| 88 | +import { books } from "../data/books"; |
| 89 | + |
| 90 | +const BooksList = () => { |
| 91 | + const loading = useLoading(books); |
| 92 | + // Do your stuff here |
| 93 | +}; |
| 94 | +``` |
| 95 | + |
| 96 | +### `useError(provider)` |
| 97 | + |
| 98 | +Triggers `read` and gives you only the `error` property from the state of the provider or selector. When the provider cache is cleaned, it automatically triggers `read` again. |
| 99 | + |
| 100 | +#### Arguments |
| 101 | + |
| 102 | +* `provider` _(Object)_: [Data Provider][data-provider] provider or selector instance. |
| 103 | + |
| 104 | +#### Returns |
| 105 | + |
| 106 | +* _(null)_/_(Error)_ - `null` when the is no error, or `Error` causing the provider `read` method rejection. |
| 107 | + |
| 108 | +#### Example |
| 109 | + |
| 110 | +```jsx |
| 111 | +import { useError } from "@data-provider/react"; |
| 112 | + |
| 113 | +import { books } from "../data/books"; |
| 114 | + |
| 115 | +const BooksList = () => { |
| 116 | + const error = useError(books); |
| 117 | + // Do your stuff here |
| 118 | +}; |
| 119 | +``` |
| 120 | + |
| 121 | +### `useRefresh(provider)` |
| 122 | + |
| 123 | +Triggers `read` method of the `provider` first time the component is rendered, and each time its cache is cleaned. This hook is used internally by the other ones, but you could also use it separatelly. |
| 124 | + |
| 125 | +## HOCs |
| 126 | + |
| 127 | +### `withDataProvider(provider, [customPropertiesNames])(Component)` |
| 128 | + |
| 129 | +This High Order Component triggers the read method of the provider and gives to the component the `data`, `loading` and `error` properties from its state. It will trigger the `read` method each time the provider cache is cleaned. |
| 130 | + |
| 131 | +Use this HOC only when you need all mentioned properties, because your component will be re-rendered any time one of them changes. If you only need one or two properties, better use one of the HOCs described bellow. |
| 132 | + |
| 133 | +#### Arguments |
| 134 | + |
| 135 | +* `provider` _(Object)_: [Data Provider][data-provider] provider or selector instance, or a `function` returning a provider or selector instance, or `undefined`. The function receives the component props as argument (Use a function when you want to `query` the provider depending of the component props). The HOC also accepts the function returning `undefined`, in which case, no provider will be used (so, you can also decide to "not connect" the component dependending of its props) |
| 136 | +* `customPropertiesNames` _(Array of Strings)_: By default, the HOC will pass to the component `data`, `loading` and `error` properties. You can change that props passing an array with new names in the same order _(`["fooData", "fooLoading", "fooError"]`)_. You can omit properties you don't want to redefine, for example: _`["fooData"]`_ will change only the `data` property. |
| 137 | + |
| 138 | +#### Examples |
| 139 | + |
| 140 | +Using a provider: |
| 141 | + |
| 142 | +```jsx |
| 143 | +import { withDataProvider } from "@data-provider/react"; |
| 144 | + |
| 145 | +import { books } from "../data/books"; |
| 146 | + |
| 147 | +const BooksList = ({ data, loading, error }) => { |
| 148 | + // Do your stuff here |
| 149 | +}; |
| 150 | + |
| 151 | +export default withDataProvider(books)(BooksList); |
| 152 | +``` |
| 153 | + |
| 154 | +With custom properties: |
| 155 | + |
| 156 | +```jsx |
| 157 | +const BooksList = ({ booksData, booksLoading, booksError }) => { |
| 158 | + // Do your stuff here |
| 159 | +}; |
| 160 | + |
| 161 | +export default withDataProvider(books, ["booksData", "booksLoading", "booksError"])(BooksList); |
| 162 | +``` |
| 163 | + |
| 164 | +Using a function: |
| 165 | + |
| 166 | +```jsx |
| 167 | +const BookDetail = ({ data, loading, error }) => { |
| 168 | + // Do your stuff here |
| 169 | +}; |
| 170 | + |
| 171 | +export default withDataProvider(({ id }) => books.query({ urlParam: { id }}))(BookDetail); |
| 172 | +``` |
| 173 | + |
| 174 | +### `withData(provider, customPropName)(Component)` |
| 175 | + |
| 176 | +This High Order Component triggers the read method of the provider and gives to the component only the `data` property from its state. It will trigger the `read` method each time the provider cache is cleaned. |
| 177 | + |
| 178 | +#### Arguments |
| 179 | + |
| 180 | +* `provider` _(Object)_: [Data Provider][data-provider] provider or selector instance, or a function as described in the [withDataProvider HOC docs](#withdataproviderprovider-custompropertiesnamescomponent) |
| 181 | +* `customPropName` _(String)_: By default, the HOC will pass to the component a `data` property. You can change that prop passing a new property name as second argument. |
| 182 | + |
| 183 | +#### Examples |
| 184 | + |
| 185 | +```jsx |
| 186 | +import { withData } from "@data-provider/react"; |
| 187 | + |
| 188 | +import { books } from "../data/books"; |
| 189 | + |
| 190 | +const BooksList = ({ data }) => { |
| 191 | + // Do your stuff here |
| 192 | +}; |
| 193 | + |
| 194 | +export default withData(books)(BooksList); |
| 195 | +``` |
| 196 | + |
| 197 | +Using custom property: |
| 198 | + |
| 199 | +```jsx |
| 200 | +const BooksList = ({ booksData }) => { |
| 201 | + // Do your stuff here |
| 202 | +}; |
| 203 | + |
| 204 | +export default withData(books, "booksData")(BooksList); |
| 205 | +``` |
| 206 | + |
| 207 | +### `withLoading(provider, customPropName)(Component)` |
| 208 | + |
| 209 | +This High Order Component triggers the read method of the provider and gives to the component only the `loading` property from its state. |
| 210 | + |
| 211 | +#### Arguments |
| 212 | + |
| 213 | +* `provider` _(Object)_: [Data Provider][data-provider] provider or selector instance, or a function as described in the [withDataProvider HOC docs](#withdataproviderprovider-custompropertiesnamescomponent) |
| 214 | +* `customPropName` _(String)_: By default, the HOC will pass to the component a `loading` property. You can change that prop passing a new property name as second argument. |
| 215 | + |
| 216 | +#### Examples |
| 217 | + |
| 218 | +```jsx |
| 219 | +import { withLoading } from "@data-provider/react"; |
| 220 | + |
| 221 | +import { books } from "../data/books"; |
| 222 | + |
| 223 | +const BooksList = ({ loading }) => { |
| 224 | + // Do your stuff here |
| 225 | +}; |
| 226 | + |
| 227 | +export default withLoading(books)(BooksList); |
| 228 | +``` |
| 229 | + |
| 230 | +Using custom property: |
| 231 | + |
| 232 | +```jsx |
| 233 | +const BooksList = ({ booksLoading }) => { |
| 234 | + // Do your stuff here |
| 235 | +}; |
| 236 | + |
| 237 | +export default withLoading(books, "booksLoading")(BooksList); |
| 238 | +``` |
| 239 | + |
| 240 | +### `withError(provider, customPropName)(Component)` |
| 241 | + |
| 242 | +This High Order Component triggers the read method of the provider and gives to the component only the `error` property from its state. It will trigger the `read` method each time the provider cache is cleaned. |
| 243 | + |
| 244 | +#### Arguments |
| 245 | + |
| 246 | +* `provider` _(Object)_: [Data Provider][data-provider] provider or selector instance, or a function as described in the [withDataProvider HOC docs](#withdataproviderprovider-custompropertiesnamescomponent) |
| 247 | +* `customPropName` _(String)_: By default, the HOC will pass to the component an `error` property. You can change that prop passing a new property name as second argument. |
| 248 | + |
| 249 | +#### Examples |
| 250 | + |
| 251 | +```jsx |
| 252 | +import { withError } from "@data-provider/react"; |
| 253 | + |
| 254 | +import { books } from "../data/books"; |
| 255 | + |
| 256 | +const BooksList = ({ error }) => { |
| 257 | + // Do your stuff here |
| 258 | +}; |
| 259 | + |
| 260 | +export default withError(books)(BooksList); |
| 261 | +``` |
| 262 | + |
| 263 | +Using custom property: |
| 264 | + |
| 265 | +```jsx |
| 266 | +const BooksList = ({ booksError }) => { |
| 267 | + // Do your stuff here |
| 268 | +}; |
| 269 | + |
| 270 | +export default withError(books, "booksError")(BooksList); |
| 271 | +``` |
| 272 | + |
| 273 | +### `withRefresh(provider)(Component)` |
| 274 | + |
| 275 | +This High Order Component triggers the `read` method of the provider each time the provider cache is cleaned. It is used internally by the rest of HOCs, but you could also use it separately. |
| 276 | + |
| 277 | +#### Arguments |
| 278 | + |
| 279 | +* `provider` _(Object)_: [Data Provider][data-provider] provider or selector instance, or a function as described in the [withDataProvider HOC docs](#withdataproviderprovider-custompropertiesnamescomponent) |
| 280 | + |
| 281 | +### `withDataProviderBranch(provider, [customPropertiesNames])(Component, LoadingComponent, ErrorComponent)` |
| 282 | + |
| 283 | +This HOC works as the already described [`withDataProvider`](#withdataproviderprovider-custompropertiesnamescomponent), but it will render one component or another depending of the result. If the provider is loading, it will render `LoadingComponent`, if it has an error, it will render `ErrorComponent` (passing the `error` property to it), or `Component` when there is no error and it is not loading (passing the `data` property to it). |
| 284 | + |
| 285 | +```jsx |
| 286 | +import { withDataProviderBranch } from "@data-provider/react"; |
| 287 | + |
| 288 | +import { books } from "../data/books"; |
| 289 | + |
| 290 | +const BooksList = ({ data }) => { |
| 291 | + // Do your stuff here |
| 292 | +}; |
| 293 | + |
| 294 | +const BooksLoading = () => { |
| 295 | + // Do your stuff here |
| 296 | +}; |
| 297 | + |
| 298 | +const BooksError = ({ error }) => { |
| 299 | + // Do your stuff here |
| 300 | +}; |
| 301 | + |
| 302 | +export default withDataProviderBranch(books)(BooksList, BooksLoading, BooksError); |
| 303 | +``` |
10 | 304 |
|
11 | 305 | ## Contributing |
12 | 306 |
|
13 | 307 | Contributors are welcome. |
14 | 308 | Please read the [contributing guidelines](.github/CONTRIBUTING.md) and [code of conduct](.github/CODE_OF_CONDUCT.md). |
15 | 309 |
|
16 | | -[data-provider-url]: https://github.com/data-provider/core |
17 | | -[data-provider-instances-docs-url]: https://github.com/data-provider/core/blob/master/docs/instances/api.md |
| 310 | +[data-provider]: https://www.data-provider.org |
| 311 | +[axios]: https://github.com/axios/axios |
| 312 | +[get-started]: https://www.data-provider.org/docs/getting-started |
| 313 | +[basic-tutorial]: https://www.data-provider.org/docs/basics-intro |
| 314 | + |
| 315 | +[react-redux-hooks]: https://react-redux.js.org/api/hooks |
18 | 316 |
|
19 | 317 | [coveralls-image]: https://coveralls.io/repos/github/data-provider/react/badge.svg |
20 | 318 | [coveralls-url]: https://coveralls.io/github/data-provider/react |
|
0 commit comments