⚠ Project Discontinuation
We regret to inform you that this project is no longer maintained. For an alternative solution, consider using React Query
React bindings for @data-provider
Set of hooks and HOCs for binding @data-provider to React components
npm i --save @data-provider/react
- withDataLoadingError
- withDataLoadedError
- withData
- withLoading
- withLoaded
- withError
- withPolling
- withDataLoadingErrorComponents
- withDataLoadedErrorComponents
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.
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.
provider
(Object): Data Provider provider or selector instance.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 for further info.
- (Array) - Array containing
data
,loading
anderror
properties, in that order.
import { useDataLoadingError } from "@data-provider/react";
import { books } from "../data/books";
const BooksList = () => {
const [data, loading, error] = useDataLoadingError(books);
// Do your stuff here
};
This hook has the same behavior and interface than the described for the useDataLoadingError
one, but it returns the data
, loaded
and error
properties from the state of the provider or selector.
Use this hook only when you don't want to rerender a Component each time the provider is loading. It will return loaded
as true
once the provider has loaded for the first time, and it will not change again. This is useful to avoid rerenders in scenarios having "pollings", for example, as it will avoid to render a "loading" each time the data is refreshed.
Take into account that the loaded
property will not be set as true
until a success read has finished, so the error may have a value, even when loaded
is false
.
- (Array) - Array containing
data
,loaded
anderror
properties, in that order.
import { useDataLoadedError } from "@data-provider/react";
import { books } from "../data/books";
const BooksList = () => {
const [data, loaded, error] = useDataLoadedError(books);
// Do your stuff here
};
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.
Arguments are the same than described for the useDataLoadingError
hook.
- (Any) - Value of the
data
property from the provider state.
import { useData } from "@data-provider/react";
import { books } from "../data/books";
const BooksList = () => {
const data = useData(books);
// Do your stuff here
};
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.
provider
(Object): Data Provider provider or selector instance.
- (Boolean) - Value of the
loading
property from the provider state.
import { useLoading } from "@data-provider/react";
import { books } from "../data/books";
const BooksList = () => {
const loading = useLoading(books);
// Do your stuff here
};
Triggers read
and gives you only the loaded
property from the state of the provider or selector. When the provider cache is cleaned, it automatically triggers read
again.
provider
(Object): Data Provider provider or selector instance.
- (Boolean) - Value of the
loaded
property from the provider state.
import { useLoaded } from "@data-provider/react";
import { books } from "../data/books";
const BooksList = () => {
const loaded = useLoaded(books);
// Do your stuff here
};
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.
provider
(Object): Data Provider provider or selector instance.
- (null)/(Error) -
null
when the is no error, orError
causing the providerread
method rejection.
import { useError } from "@data-provider/react";
import { books } from "../data/books";
const BooksList = () => {
const error = useError(books);
// Do your stuff here
};
Triggers cleanDependenciesCache
method of the provider each interval
miliseconds while the component is "alive". It can be used in multiple components at the same time for the same provider. In that case, the used interval will be the lower one, and it will be recalculated each time a component is added or removed.
This hook can also be used with Data Provider selectors, as it will clean the cache of all selector dependencies. So, if you are using a selector combining data from two axios providers, for example, it will result in repeating both provider http requests and recalculating the selector result every defined interval.
provider
(Object): Data Provider provider or selector instance.interval
(Number): Interval in miliseconds to clean the provider dependencies cache. Default is 5000.options
(Object): Options object that will be passed as is to thecleanCache
method of providers orcleanDependenciesCache
method of selectors. Check the data-provider API documentation for further info. Options can be defined as second argument if interval is omitted.
import { useData, usePolling } from "@data-provider/react";
import { books } from "../data/books";
const BooksList = () => {
const data = useData(books);
usePolling(books, 3000);
// Do your stuff here. Books will fetched again from server every 3 seconds
};
import { useData, usePolling } from "@data-provider/react";
import { booksAndAuthors, books } from "../data/books";
const BooksList = () => {
const data = useData(books);
usePolling(booksAndAuthors, {
except: [books]
});
// Do your stuff here. booksAndAuthors selector dependencies will fetched again from server every 3 seconds, except the "books" provider.
};
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.
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.
provider
(Object): Data Provider provider or selector instance, or afunction
returning a provider or selector instance, orundefined
. The function receives the component props as argument (Use a function when you want toquery
the provider depending of the component props). The HOC also accepts the function returningundefined
, in which case, no provider will be used (so, you can also decide to "not connect" the component dependending of its props)customPropertiesNames
(Array of Strings): By default, the HOC will pass to the componentdata
,loading
anderror
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 thedata
property.
Using a provider:
import { withDataLoadingError } from "@data-provider/react";
import { books } from "../data/books";
const BooksList = ({ data, loading, error }) => {
// Do your stuff here
};
export default withDataLoadingError(books)(BooksList);
With custom properties:
const BooksList = ({ booksData, booksLoading, booksError }) => {
// Do your stuff here
};
export default withDataLoadingError(books, ["booksData", "booksLoading", "booksError"])(BooksList);
Using a function:
const BookDetail = ({ data, loading, error }) => {
// Do your stuff here
};
export default withDataLoadingError(({ id }) => books.query({ urlParam: { id }}))(BookDetail);
This hoc has the same behavior and interface than the described for the withDataLoadingError
one, but it provides the data
, loaded
and error
properties from the state.
Use this hook only when you don't want to rerender a Component each time the provider is loading. It will return loaded
as true
once the provider has loaded for the first time, and it will not change again. This is useful to avoid rerenders in scenarios having "pollings", for example, as it will avoid to render a "loading" each time the data is refreshed.
Take into account that the loaded
property will not be set as true
until a success read has finished, so the error may have a value, even when loaded
is false
.
Using a provider:
import { withDataLoadedError } from "@data-provider/react";
import { books } from "../data/books";
const BooksList = ({ data, loaded, error }) => {
// Do your stuff here
};
export default withDataLoadedError(books)(BooksList);
With custom properties:
const BooksList = ({ booksData, booksAreLoaded, booksError }) => {
// Do your stuff here
};
export default withDataLoadedError(books, ["booksData", "booksAreLoaded", "booksError"])(BooksList);
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.
provider
(Object): Data Provider provider or selector instance, or a function as described in the withDataLoadingError HOC docscustomPropName
(String): By default, the HOC will pass to the component adata
property. You can change that prop passing a new property name as second argument.
import { withData } from "@data-provider/react";
import { books } from "../data/books";
const BooksList = ({ data }) => {
// Do your stuff here
};
export default withData(books)(BooksList);
Using custom property:
const BooksList = ({ booksData }) => {
// Do your stuff here
};
export default withData(books, "booksData")(BooksList);
This High Order Component triggers the read method of the provider and gives to the component only the loading
property from its state.
provider
(Object): Data Provider provider or selector instance, or a function as described in the withDataLoadingError HOC docscustomPropName
(String): By default, the HOC will pass to the component aloading
property. You can change that prop passing a new property name as second argument.
import { withLoading } from "@data-provider/react";
import { books } from "../data/books";
const BooksList = ({ loading }) => {
// Do your stuff here
};
export default withLoading(books)(BooksList);
Using custom property:
const BooksList = ({ booksLoading }) => {
// Do your stuff here
};
export default withLoading(books, "booksLoading")(BooksList);
This High Order Component triggers the read method of the provider and gives to the component only the loaded
property from its state.
provider
(Object): Data Provider provider or selector instance, or a function as described in the withDataLoadingError HOC docscustomPropName
(String): By default, the HOC will pass to the component aloaded
property. You can change that prop passing a new property name as second argument.
import { withLoaded } from "@data-provider/react";
import { books } from "../data/books";
const BooksList = ({ loaded }) => {
// Do your stuff here
};
export default withLoaded(books)(BooksList);
Using custom property:
const BooksList = ({ booksLoaded }) => {
// Do your stuff here
};
export default withLoaded(books, "booksLoaded")(BooksList);
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.
provider
(Object): Data Provider provider or selector instance, or a function as described in the withDataLoadingError HOC docscustomPropName
(String): By default, the HOC will pass to the component anerror
property. You can change that prop passing a new property name as second argument.
import { withError } from "@data-provider/react";
import { books } from "../data/books";
const BooksList = ({ error }) => {
// Do your stuff here
};
export default withError(books)(BooksList);
Using custom property:
const BooksList = ({ booksError }) => {
// Do your stuff here
};
export default withError(books, "booksError")(BooksList);
This High Order Component works as the hook usePolling
described above.
provider
(Object): Data Provider provider or selector instance, or a function as described in the withDataLoadingError HOC docsinterval
(Number): Interval in miliseconds to clean the provider dependencies cache. Default is 5000.options
(Object): Options object that will be passed as is to thecleanCache
method of providers orcleanDependenciesCache
method of selectors. Check the data-provider API documentation for further info. Options can be defined as second argument if interval is omitted.
import { withData, withPolling } from "@data-provider/react";
import { books } from "../data/books";
const BooksList = ({ data }) => {
// Do your stuff here. Books data will fetched again from server every 3 seconds
};
export default withPolling(books, 3000)(withData(books)(BooksList));
provider
(Object): Data Provider provider or selector instance, or a function as described in the withDataLoadingError HOC docs
withDataLoadingErrorComponents(provider, [customPropertiesNames])(Component, LoadingComponent, ErrorComponent)
This HOC works as the already described withDataLoadingError
, 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 it will render Component
when there is no error and it is not loading (passing the data
property to it).
import { withDataLoadingErrorComponents } from "@data-provider/react";
import { books } from "../data/books";
const BooksList = ({ data }) => {
// Do your stuff here
};
const BooksLoading = () => {
// Do your stuff here
};
const BooksError = ({ error }) => {
// Do your stuff here
};
export default withDataLoadingErrorComponents(books)(BooksList, BooksLoading, BooksError);
withDataLoadedErrorComponents(provider, [customPropertiesNames])(Component, NotLoadedComponent, ErrorComponent)
This HOC works as the already described withDataLoadedError
, but it will render one component or another depending of the result. If the provider has an error, it will render ErrorComponent
(passing the error
property to it), if it has not loaded, it will render NotLoadedComponent
, or it will render Component
when there is no error and it has loaded (passing the data
property to it).
import { withDataLoadedErrorComponents } from "@data-provider/react";
import { books } from "../data/books";
const BooksList = ({ data }) => {
// Do your stuff here
};
const BooksNotLoaded = () => {
// Do your stuff here
};
const BooksError = ({ error }) => {
// Do your stuff here
};
export default withDataLoadedErrorComponents(books)(BooksList, BooksNotLoaded, BooksError);
Contributors are welcome. Please read the contributing guidelines and code of conduct.