React Easy Universal
Universal Routing & Rendering with React & Redux was too hard. Now it's easy.
Status
Proof of concept. Needs testing. Kick the tires.
What's included?
- Redux
- React Router
- Automatic syncing between Redux and React Router via react-router-redux
- History
- Debugging support
Getting Started
You'll need to create two files:
client.js
:
; // Note: There are two versions of the client: bundled and source.// If you want to build from source, try `react-easy-universal/client-src`.; // returns a function that must be invoked to trigger renderconst app = ; // use all the defaults // The app function will return your store so you can dispatch actions.const store = ; // Do stuff in your client app to trigger re-renders.// e.g., subscribe to server updates, etc...store;
server.js
:
;;; // Passing in the express app lets it know you want the server// version, and it wires up the routes automaticallyconst app = ; app; const port = processenvAPP_PORT || 3000; app;
Note: There is nothing exported from react-easy-universal
by default. You must
use one of the client or the server. They are two different builds in order to
save on client download time.
Defining Your Routes
Use this module instead of depending directly on React Router, and we'll worry about keeping all the version dependencies compatible and in-sync for you.
; ;; // It expects a factory function that it can inject dependencies into. { return <Router> <Route path="/" component= /> <Route path="/test-data" component= /> </Router> ;};
n
busted way)
Why is this needed? (AKA, the old Universal routing & rendering with React and Redux is pretty great. For serious app projects, it can save you a ton of time, but if you have ever tried configuring it yourself, you'll know, it's a lot harder than it should be. Just to give you an idea of how complicated it can be, here's the example from redux-simple-router
, which is AFAIK, the easiest way to configure this stuff right now:
const reducer = // Sync dispatched route actions to the historyconst reduxRouterMiddleware = const createStoreWithMiddleware = createStore const store = // Required for replaying actions from devtools to workreduxRouterMiddleware ReactDOM
That's six dependencies that you have to manage in your own app, and good luck getting all the right versions that will play together nicely. And this is just the client side. The code above doesn't work on the server. To get that going, you'll have to create an Express route handler that manually calls match()
from react-router
, checks for errors & redirects, and maybe renders the document. It looks something like this:
;; ;;; ;; const store = ;const routes = ;const initialState = store; { ;};
There are a few other files you need to wire up that I won't list, for brevity's sake.
It took me two days to get these examples working in one of my own projects. 2 days of fiddling with dependencies, copying the exact versions out of the example repositories and into my package.json
. These projects are evolving quickly, and the documentation examples can't be relied on to work.
So, you could keep track of all these dependency versions yourself (and they're all being rapidly updated) -- or, you could use this library, plug in your routes & reducers, and get on with building an actual application instead of chasing all the moving parts around.
Advanced Configuration (note: unfinished)
Need to customize layouts, the root React Node, the root route, and so on? No problem. Just make your wire-app.js
factory configurable:
; ;; const wireApp = React app rootID // default: 'root' rootRoute // default: '/' renderLayout // Skeleton DOM render template for the server-side. Default: Barebones ES6 template ; ;
Contributing
There are some handy scripts for contributors, if you'd like to pitch in:
Dev console
There's a dev console available that will show you any lint errors or test failures on file saves:
npm run watch
Debugging
To run the debugger, first, make sure it's installed:
npm install -g iron-node
Then:
npm run debug:test