react-router-fetch
module to loop through matched react router handler to call fetch methods for specifying data needs at the handler level
Why?
I wanted a nice and contained module to be able to initiate requests for route handlers in an app, so that the data would be loaded before the route handler was rendered. This module doesn't accomplish that on it's own, but can be used as a part of a solution to that problem.
Usage
react router fetch wraps react-router-config matchRoutes
. It then will go through the routes in a similar fashion as the README
suggests.
const App = <div /> static { return { } } { return <div>Home</div> } const routes = component: App routes: path: '/' exact: true component: Home
in a component you would want to pass the this.props.location
from react-router in order to have full access to that in the static fetch method on the component.
Specifying route data needs
This allows you to specify at the route handler level what data that route needs via a static fetch method. The fetching itself should be wired up to redux via thunks, or whatever way you want to handle that. the only requirement is that the static method returns a promise.
static { //return a promise to be resolved later, superagent as an example return } { //your stuff }
This module is intended to be a building block for other modules or as a low level part of your application.
Using in a top level component
Assuming you have a top level component, you can export it using withRouter
to get the location prop injected into your component.
state = isAppFetching: false appFetchingError: null { this } { const current = `` const next = `` if current === next return this } { return !nextStateisAppFetching } { const dispatch location = props this //maybe show a progress bar somewhere outside of react? go nuts!! } { //do something with isAppFetching for the first render if single page app. // after the first render, the page contents will stay put until the next route's data is ready to go, so you'll have to do something outside of this. return ... } const connectedApp = AppconnectedApp