Final Questions
- is mixin worth using for state machine type of things?
you can use React.createClass and mixins will continue to work
- server side rendering?Three elements. There isn’t an idiomatic way to do server-side rendering. People are still figuring it out and do it differently.
Three things you need to deal with:
- Getting data before you render (render is not async, you cannot wait for data arrive after you have your layout HTML) a.k.a. Hydrating
- Get the data to the client (the handoff)
- The client needs to render with that data (dehydrate the data)
Some people render everything but the data. Then the client fetches the data.
Demoes how it would look like.Repo is the React Router Mega Demo
// **** app/index.html
<!doctype html public "restroom">
<meta charset="utf-8"/>
<title>React Router Mega Demo</title>
<link rel="stylesheet" href="/styles.css"/>
<body>
<div id="app">¡HTML!</div>
<script>
__DATA__ = ¡DATA!;
</script>
<script src="/js/main.js"></script>
Move to server.js
// **** server.js ****
...
router.run((Handler, state) => {
if (state.routes[0].name === 'not-found') {
var html = React.renderToStaticMarkup(<Handler/>);
cb({notFound: true}, html);
return;
}
// fetches data first (in async, non blocking way)
fetchData(token, state).then((data) => {
// then hands of, "dehydrates"
var clientHandoff = { token, data: cache.clean(token) };
// instead of rendering to the DOM
// it just renderes a string
var html = React.renderToString(<Handler data={data} />);
var output = indexHTML.
replace(htmlRegex, html).
replace(dataRegex, JSON.stringify(clientHandoff));
cb(null, output, token);
});
});
....
Then shows how the client fetches and uses the data. Explains the caching. The server passes the data as a global javascript object. Then the client grabs the data, puts into the cache. Then the ajax calls only deal with the cache. So the client handoff is just this
- what about flux?
<script>__DATA__ = ¡DATA!;</script>
. more complicated because of singletons. you would rehydrate and dehydrate into the stores.
Your data API, can be whatever you want. You can have a Node UI layer in between.