redial
Universal data fetching and route lifecycle management for React etc.
$ npm install --save redial
Why?
When using something like React Router, you'll want to ensure that all data for a set of routes is prefetched on the server before attempting to render.
However, as your application grows, you're likely to discover the need for more advanced route lifecycle management.
For example, you might want to separate mandatory data dependencies from those that are allowed to fail. You might want to defer certain data fetching operations to the client, particularly in the interest of server-side performance. You might also want to dispatch page load events once all data fetching has completed on the client.
In order to accommodate these scenarios, the ability to define and trigger your own custom route-level lifecycle hooks becomes incredibly important.
Providing lifecycle hooks
The @provideHooks
decorator allows you to define hooks for your custom lifecycle events, returning promises if any asynchronous operations need to be performed. When using something like React Router, you'll want to decorate your route handlers rather than lower level components.
For example:
; ;; @ { return <div>...</div>; }
If you'd prefer to avoid using decorators, you can use provideHooks
as a plain old function:
const hooks = ; { return <div>...</div>; } hooksMyRouteHandler;
Triggering lifecycle events
Once you've decorated your components, you can then use the trigger
function to initiate an event for an arbitrary array of components, or even a single component if required. Since hooks tend to be asynchronous, this operation always returns a promise.
For example, when fetching data before rendering on the server:
; const locals = some: 'data' more: 'stuff'; ;
Dynamic locals
If you need to calculate different locals for each lifecycle hook, you can provide a function instead of an object. This function is then executed once per lifecycle hook, with a static reference to the component provided as an argument.
For example, this would allow you to calculate whether a component is being rendered for the first time and pass the result in via the locals object:
const getLocals = isFirstRender: prevComponents === -1; ;
Example usage with React Router and Redux
When server rendering with React Router (or using the same technique to render on the client), the renderProps
object provided to the match
callback has an array of routes, each of which has a component attached. You're also likely to want to pass some information from the router to your lifecycle hooks.
In order to dispatch actions from within your hooks, you'll want to pass in a reference to your store's dispatch
function. This works especially well with redux-thunk to ensure your async actions return promises.
Example server usage
; ;;;;;; // Your app's reducer and routes:;; // Render the app server-side for a given path: { // Set up Redux (note: this API requires redux@>=3.1.0): const store = ; const dispatch getState = store; // Set up history for router: const history = ; // Match routes based on history object: ;};
Example client usage
; ;;;;;; // Your app's reducer and routes:;; // Render the app client-side to a given container element: // Your server rendered response needs to expose the state of the store, e.g. // <script> // window.INITIAL_STATE = <%- require('serialize-javascript')(state)%> // </script> const initialState = windowINITIAL_STATE; // Set up Redux (note: this API requires redux@>=3.1.0): const store = ; const dispatch = store; // Listen for route changes on the browser history instance: browserHistory; // Render app with Redux and router context to container element: ;;
Boilerplates using redial
Related projects
- React Resolver by @ericclemmons
- React Transmit by @rygu
- AsyncProps for React Router by @ryanflorence
- GroundControl by @nickdreckshage
- React Async by @andreypopp