react-use-fetch-factory
A factory pattern for react hooks to abstract away your tedious data fetching and selecting logic.
Install
npm i react-use-fetch-factory
or
yarn add react-use-fetch-factory
API
The Hook takes three parameters.
selector
The selector you are using to retrieve your data from the redux state.fetchActionCreator
An action creator that returns an action to trigger a fetch inside a saga, thunk, etc.emptyArrayAsFalsy
The Hook will check if there is data in the redux store. If there is data found, the fetch will not be executed. Only if the selector returnsnull
orundefined
the fetch action will be triggered. Setting this parameter totrue
will include an empty array asfalsy
and trigger the fetch action.
Usage
The best usage is to build your own custom Hook which uses the useFetchFactory
Hook, like so:
;; { return ;}
With you custom hook you can then import your selecting and fetching logic with one line:
import * as React from 'react';import useProducts from '../hooks/fetching'; { const products = ; return <List> products </List> ;}
Pretty neat, eh?
What this is not
This hook is just an abstraction for the data selecting and fetching logic you need to build inside your components. This is not handling the fetching and selecting logic itself. You need to provide that yourself via selectors and redux middleware with sagas, thunks, etc.
Motivation & How it works
Hooks are a great way to make our components leaner. With the release of react-redux 7 we
can finally use Hooks to provide our store to components. Previously we had to use connect
to
bind our components to the redux store which ended in a complicated boilerplate looking higher
order component. This became particular annoying when dealing with selectors and data from
the server, because all of this check, fetching and selecting usually happened inside the component
code:
import * as React from 'react';import connect from 'react-redux';import fetchTodos toggleTodo getTodosSelector from '../ducks/todos'; Component { ifthispropstodoslength === 0 thisprops; } { return <List> thispropstodos </List> ; } { return todos: } { return fetchTodos addTodo toggleTodo } mapStateToProps mapDispatchToPropsTodoApp
This is a lot of code for just displaying a list of Todos. We want to check if there are todos when the component mounts and if not, we fetch them (e.g. via a saga, thunk or whatever you like) And not only is this a lot of code, but we'll also repeat ourselves over and over again the more components we have that just fetch some kind of data and display it.
With hooks we can get leaner:
import React useEffect from 'react';import useDispatch useSelector from 'react-redux';import fetchTodos toggleTodo getTodosSelector from '../ducks/todos'; { const dispatch = ; const todos = ; ; return <List> todos </List> ;}
Way cleaner. But we also will repeat ourselves in this case. The only thing that will change if we fetch different data is the selector, the dispatched fetching action and the ui representation. But wait. We can build our own Hooks! So let's abstract all this away:
import useEffect from 'react';import useDispatch useSelector from 'react-redux'; { const dispatch = ; const data = ; ; return data;}
And there it is! Our own custom data fetching and selecting Hook!
Found an issue or bug?
Please open a pull request or an issue and contribute.