@fpc/hooks v1.3.3
@fpc/hooks
Collection of general purpose React Hooks.
API
useArrayState
import { useArrayState } from '@fpc/hooks';
const [data, updateData] = useArrayState(initialArg);initialArg must be an iterable
or a function that returns an iterable that is used to initialize data
for the initial render.
updateData accepts an iterable iter or a function fn
that returns an iterable.
- In the former case a re-render will be triggered and
the new data will be
[ ...data, ...iter ]. - If
updateDatareceivesfnthe data in the next render will befn(data).
useFetch
import { useFetch } from '@fpc/hooks';
const [fetchResponsePromise, refetch] = useFetch(resource, init);resource, init and fetchResponsePromise have the same semantic of browser
fetch,
refetch is a function that can be called to fetch again the given resource
and trigger a re-render.
⚠ Be careful about render loops ⚠
import React from 'react';
import { useFetch, usePromise } from '@fpc/hooks';
function App() {
const [responsePromise, refetch] = useFetch(myResource);
const [res, err, status] = usePromise(responsePromise.then(res => res.text()));
return <p>{res}</p>;
}This will cause a render loop because responsePromise.then() will produce a new
Promise at each render that will trigger a re-render.
What you need to do is:
- Wrap the promise in a function
() => responsePromise.then(res => res.text()) - Pass the reference of the promise before
theninusePromise's dependency list
function App() {
const [response, refetch] = useFetch(myResource);
const [res, err, status] = usePromise(() =>
response.then(res => res.text())
, [response]);
return <p>{res}</p>;
}useJsonFetch
As you see in useFetch deal with promises can be
non-trivial, but since most of the time you just want response.text()
or response.json() these utilities make life a bit easier:
import React from 'react';
import { useJsonFetch } from '@fpc/hooks';
function App() {
const [json, err, status, refetch] = useJsonFetch(myResource);
return <p>{JSON.stringify(json)}</p>;
}jsonis eitherundefinedor the parsed responseerrisundefinedor contains an errorstatusis one of'pending','resolved'or'rejected'refetchcan be called to re-trigger the network request and re-render
useLazy
import { useLazy } from '@fpc/hooks';
const [value, update] = useLazy(fn, initialValue);During the initial render value will be initialValue (undefined if not passed).
Calling the update function will trigger a re-render with value setted to fn().
useLazyPromise
import { useLazyPromise } from '@fpc/hooks';
const [value, error, state, update] = useLazyPromise(fn, initialValue);fnmust be a function that returns a thenableinitialValueis optional andundefinedby default, it's used asvaluefor the first rendervalueisinitialValueuntilupdateis called, then will beundefineduntil the promise is resolvederroris eitherundefinedor contains the promise's errorstateis'idle'untilupdateis called for the first time, then is'pending'and finally'resolved'or'rejected'updateis a function that will trigger a call tofnand a re-render
useObjectState
import { useObjectState } from '@fpc/hooks';
const [state, updateState] = useObjectState(initialArg);initialArg must be an object or a function that returns an object
that is used to initialize state for the initial render.
updateState accepts an object obj or a function fn
that returns an object.
- In the former case a re-render will be triggered and
the new
statewill be{ ...state, ...obj }. - If
updateStatereceivesfnthestatein the next render will befn(data).
usePollingPromise
import { usePollingPromise } from '@fpc/hooks';
const [value, error, status, update] = usePollingPromise(fn, initialValue);fnmust be a function that returns a thenableinitialValueis optional andundefinedby default, it's used asvaluefor the first rendervalueisinitialValueuntil the promise is resolvederroris eitherundefinedor contains the promise's errorstatusis one of'pending','resolved'or'rejected'updatewill trigger a new call tofn, restart the promise resolving and re-render
usePollingValue
import { usePollingValue } from '@fpc/hooks';
const [value, update] = usePollingValue(fn);During the first render value is fn(). The update function can be called
to produce a new value and trigger a re-render.
usePromise
import { usePromise } from '@fpc/hooks';
const [value, error, status] = usePromise(task, deps);taskcan be a thenable or a function that returns a thenable.depsis an optional dependency array, just like the useEffect's onevalueisundefinedor contains the value of the promiseerroris eitherundefinedor contains the promise's errorstatusis one of'pending','resolved'or'rejected'
useTextFetch
import React from 'react';
import { useTextFetch } from '@fpc/hooks';
function App() {
const [text, err, status, refetch] = useTextFetch(myResource);
return <p>{text}</p>;
}textis eitherundefinedor the response bodyerrisundefinedor contains an errorstatusis one of'pending','resolved'or'rejected'refetchcan be called to re-trigger the network request and re-render
4 years ago
4 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago