16. Isomorphic/Universal React
const appHtml = ReactDOMServer.renderToString(component);
Server Side
Browser Side
ReactDOM.render(component,
window.document.getElementById("appRoot"));
34. class Foo extends React.Component{
componentDidMount() {
// do DOM manipulation with jQuery
}
componentDidUpdate() {
// do DOM manipulation with jQuery
}
render() {
// render JSX for react
}
}
react render
jQuery render
react render
jQuery render
48. Server Rendering Process
Fetching State
Rendering Components
Render Dehydrated State
fetchState().then(render)
ReactDOMServer.renderToString(...)
<script>
window.initState = ...
</script>
49. Page Should Not Be Component
class Page {
getComponents() {
return [
Header,
Foo,
Bar,
Footer
];
}
}
50. Server Rendering Process Improved
Start Fetching State
Rendering Component
Render Dehydrated State
Render Dehydrated State
Rendering Component
Rendering Component
Render Dehydrated State
Push After Every Render
All my experience is about trade off between complexity and performance.
&lt;number&gt;
Hulu is Subscription-based video provider.
&lt;number&gt;
&lt;number&gt;
&lt;number&gt;
The migration of tech stack.
Not just because React & Redux is cool and new, but it solves OUR specific problems.
&lt;number&gt;
jQuery is created for a reason, but React solves the same problem in more understandable way.
&lt;number&gt;
jQuery code is so easy to be written into a mess.
&lt;number&gt;
VirtualDOM magic of React/JSX
&lt;number&gt;
The ensense of React and Redux
&lt;number&gt;
React is actually implementing web-component.
HTML, CSS and JS is consolidated into one component.
&lt;number&gt;
Just write one component which can be rendered in browser OR in server. It doesn’t matter.
Isomorphic(同构) React
&lt;number&gt;
React is just another jQuery. d It helps to build component, but not work alone for big app.
&lt;number&gt;
Strictly, Model should not communicate directly with View. But, it is easy to be violated.
&lt;number&gt;
Flux is uni-direction flow.
It doesn’t make sense to match parts between MVC and Flux
&lt;number&gt;
&lt;number&gt;
Redux is a better flux/fluxable.
&lt;number&gt;
Redux is a better flux/fluxable.
&lt;number&gt;
View: What content should I rendered?
Actions: What ”events” can I respond?
Reducer: How to update state according to actions?
State Key: The sub state key controlled by this component
&lt;number&gt;
Data Flow
&lt;number&gt;
React & Redux enforce functional programming model; isomorphic javascript. Better data management.
&lt;number&gt;
React & Redux enforce functional programming model; isomorphic JavaScript.
All state resides in singleton store.
&lt;number&gt;
From perspective of architecture, we should enforce constraints instead of just asking people to follow conventions.
React & Redux enforce functional programming model; isomorphic JavaScript. Better data management.
&lt;number&gt;
Optimizely operates perfectly with jQuery, but is not designed for React.
&lt;number&gt;
Rehydrated HTML must exactly match HTML from server. Otherwise, user will see UI flash.
React do this by checking checksum attribute in generated HTML.
&lt;number&gt;
Work flow of first page access.
&lt;number&gt;
One piece JavaScript for the whole application might be too big. Need to be chunked.
&lt;number&gt;
In Single Page Application with chunked code, each page need to reset singleton store to enable new reducer and new stae.
&lt;number&gt;
React is not designed for Server according to FB.
But, there are some tricks to make it faster.
&lt;number&gt;
&lt;number&gt;
Even React helps us to avoid actual DOM manipulation time, it is still a waste if Virtual DOM is actually same in re-rendering.
&lt;number&gt;
&lt;number&gt;
React-Router-Redux maps URL path to Page Component. Each Page Component is actually a React Component. Has to be rendered in sync way.
&lt;number&gt;
&lt;number&gt;
Page should just declare which components it contains, and let framework do the incremental rendering.
&lt;number&gt;