A simple automated data refetcher for using react-redux
. The basic use case is when you have some data that needs to be refetched anytime there is a change in some of the underlying properties. However, often times there is no point in refetching the data as the component which renders it may not be mounted, so you end up repeating the same pattern of
class ExpensiveComponent extends Component {
componentWillUpdate(newProps, newState){
if(this.shouldRefetch(this.props, newProps)){
this.refetchingFunction()
}
}
shouldRefetch(oldProps, newProps){
return oldProps.prop1 !== newProps.prop1
|| oldProps.prop2 !== newProps.prop2
...
}
}
within every component that renders some server side data.
To install via npm, run
npm install --save redux-refetch
import connect from 'redux-refetch'
class ExpensiveComponent extends Component {
render(){
...
}
}
mapStateToDependencies(state, ownProps) {
return {
dependency1: dependency1Selector(state),
...
}
}
mapDispatchToRefetch(state, ownProps) {
return {
refetchFunction,
refetchFunction2,
...
}
}
export default connect(
mapStateToProps,
mapStateToDependencies,
mapDispatchToProps,
mapDispatchToRefetch
)(ExpensiveComponent)
mapStateToProps
and mapDispatchToProps
are the same as they are within react-redux.
Note: You cannot use the keys
__dependencies
or__refetch
for any props, both manually supplied props i.e.<Component prop={'prop'}/>
and when creating themapStateToProps
ormapDispatchToProps
function. These are interally used keys when building the container.
connect(?mapStateToProps, ?mapStateToDependencies, ?mapDispatchToProps, ?mapDispatchToRefetch, ?options)
The only part of this package. It is an enhanced version of react-redux
connect. It not only allows you to add redux action creators and properties from state as props
within a react component, but it also allows you to ensure that, when one of the dependencies given in mapStateToDependencies
, all of the refetching functions are called too.
-
[
mapStateToProps(state, [ownProps])
] (Function): This is the same as inreact-redux
. Documentation for that can be found here -
[
mapStateToDependencies(state, [ownProps])
] (Function): This function returns an object of all dependencies for the refetcher. It takes the redux state and the props supplied to the component as arguments and you should return an object containing all of the dependencies. When any of the dependencies supplied changes then all of the functions defined inmapDispatchToRefetch
will be called. -
[
mapDispatchToProps(state, [ownProps])
] (Function): This is the same as inreact-redux
. Documentation for that can be found here -
[
mapDispatchToRefetch(state, [ownProps])
] (Function): A function which takes both the redux state and the props supplied when rendering the component. It should return an object with each of a functions to call when the any of the dependencies defined inmapStateToDependencies
change. -
[
options
] (Object): If specified customizes the connector for bothredux-refetch
andreact-redux
. Possible options are as shown below.- [
equalityCheck
] (Function): If supplied will test whether the dependencies have changed. If this argument is not supplied then it will default to===
. - [
mergeProps
] (Function): If specified, it is called with the results ofmapStateToProps
,mapDispatchToProps
andprops
and expects the final props object as a return. More documentation on this can be found here. - [
connectOptions
] (Object): These are the options that get passed intoconnect
fromreact-redux
. Again more complete and well rounded documentation can be found here
- [
Feel free to open a PR or submit an issue if you want to suggest or improve something