Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Redux and React Server Rendering
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
hui.liu
April 10, 2016
Technology
120
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Redux and React Server Rendering
hui.liu
April 10, 2016
More Decks by hui.liu
See All by hui.liu
How to build an online IDE with React
hulufei
0
540
利用Grunt打造前端工作流
hulufei
5
1.2k
Other Decks in Technology
See All in Technology
LanceDB入門
mocobeta
8
540
Sets in Go
ramalho
1
770
【CEDEC2026】『GRANBLUE FANTASY: Relink - Endless Ragnarok』のバトル制作事例 ~最高のキャラゲーを目指して~
cygames
PRO
0
300
20260804_Q4AzureUpdateBite_FabricDataAgentの精度を高める設計.pdf
matayuuu
1
140
モダンフロントエンド 開発研修
recruitengineers
PRO
4
660
サイバー捜査員研修(後半)
nomizone
1
880
MIRU 2026 チュートリアル
keisuke198619
0
930
事業価値と Engineering 2026年度版
recruitengineers
PRO
50
24k
【AG-UI × A2UI × MCP Apps】Generative UIをやさしく解説する
nrinetcom
PRO
1
130
Digitization部 紹介資料
sansan33
PRO
2
7.7k
Flutterをカメラで動かしたかった話
sony
1
140
名刺メーカーDevグループ 紹介資料
sansan33
PRO
0
1.2k
Featured
See All Featured
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.5k
Color Theory Basics | Prateek | Gurzu
gurzu
0
420
Google's AI Overviews - The New Search
badams
0
1.1k
KATA
mclloyd
PRO
35
15k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
240
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.2k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
410
So, you think you're a good person
axbom
PRO
2
2.1k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
610
Design in an AI World
tapps
1
280
Transcript
Redux Ө React ๐ۓ ᒒວ
ڝᬄ (@hulufei)
Why Server Rendering • Single Page Application (SPA) • Search
Engine Indexability (SEO)
Universal JavaScript Server Browser SPA Rendering
–Nick Dreckshage “react made it extremely easy.”
React Virtual DOM Server Browser SPA <App /> <div>…</div> renderToString()
UI Җ f(state)
None
– reactjs/redux “Redux is a predictable state container for JavaScript
apps.”
Redux Basic • Action • Reducer • Store
// Default function actionCreator() { return { type: 'ACTION_TYPE_CONSTANT', payload:
'payload' } } Action { type: ‘TYPE_CONSTANT’, payload: payload } thunk middleware promise middleware // Thunk function actionCreator() { return (dispatch) => { dispatch({ type: 'ACTION_TYPE_CONSTANT', payload: 'xx' }) // Async operation } } // Promise function actionCreator() { return { type: 'ACTION_TYPE_CONSTANT', payload: Promise.resolve(‘payload') } }
Reducer function reducer(state, action) { switch(action.type) { case 'ACTION_TYPE_CONSTANT': //
Update state return newState; default: return state; } } (previousState, action) => newState
Store import { createStore } from 'redux' import rootReducer from
'./reducers' let store = createStore(rootReducer) ӞӻଫአํӬՐํӞӻ Store // store.getState() ᬬࢧ State ᇫா // store.dispatch(action) ݎ reducer ๅෛ State // store.subscribe(listener) ፊލ State ๅෛ
Data Flow • action ฎӞӻ۱ތ { type, payload } ጱ
• reducer ڍහ᭗ᬦ store.dispatch(action) ݎ • reducer ڍහളݑ (state, action) ӷӻ݇හ • reducer ڍහڣෙ action.type ᆐݸ॒ቘଫጱ action.payload හഝๅ ෛᇫா҅ᬬࢧӞӻෛጱ state • store.subscribe(listener) ፊލ state ๅෛ
react-redux // index.js import { render } from 'react-dom'; import
{ Provider } from 'react-redux'; import App from './app'; render( <Provider store={store}> <App /> </Provider>, document.getElementById('root') ); // app.js class App extends Component { ... } export default connect( mapStateToProps, mapDispatchProps )(App); <Provider /> connect const mapStateToProps = (state) => ({ propA: stateA, … }) const mapDispatchProps = (dispatch) => ({ propDoAction: (args) => dispatch(actionCreator(args)) ... })
Server Rendering Server Browser SPA UI = f(state) initialState Rendering
Server Rendering import { renderToString } from 'react-dom/server'; function renderFullPage(html,
initialState) { return ` <!DOCTYPE html> ... <div id=“root”>${html}</div> <script> window.__INITIAL_STATE__ = ${JSON.stringify(initialState)}; </script> ... ` } app.use((req, res) => { store.dispatch(fetchActionCreator()) .then(_ => { const html = renderToString( <Provider store={store}> <App /> </Provider> ); res.end(renderFullPage(html, store.getState())); }); }); }); Browser SPA const initialState = window.__INITIAL_STATE__; const store = createStore(rootReducer, initialState); Browser SPA componentDidMount() { store.dispatch(fetchActionCreator()); }
None
react-router (Client) import { Route, IndexRoute } from 'react-router'; const
Container = (props) => <div>{props.children}</div>; const routes = ( <Route path="/" component={Container} > <IndexRoute component={App} /> <Route path=“path/:other” component={Other} /> </Route> ); export default routes;
react-router (Server) import { renderToString } from 'react-dom/server' import {
match, RouterContext } from 'react-router' import routes from './routes' function renderFullPage(html, initialState) { … } app.use((req, res) => { match({ routes, location: req.url }, (error, redirectLocation, renderProps) => { if (error) { res.status(500).send(error.message) } else if (redirectLocation) { res.redirect(302, redirectLocation.pathname + redirectLocation.search) } else if (renderProps) { store.dispatch(fetchActionCreator()) .then(_ => { const html = renderToString( <Provider store={store}> <RoutingContext {...renderProps} /> </Provider> ); res.end(renderFullPage(html, store.getState())); }); } else { res.status(404).send('Not found') } }) }) match({ routes, location: req.url }, (error, redirectLocation, renderProps) => { import routes from './routes' <RoutingContext {...renderProps} /> Browser SPA <Route path="/" component={Container} > <IndexRoute component={App} /> <Route path=“path/:other” component={Other} /> </Route>
– reactjs/react-router “Knowing what code should run on the server
and on the client is important to using React in a universal app.”
Client or Server • componentDidMount() Invoked once, only on the
client (not on the server) • <Link /> or <a /> • isomorphic-fetch
Server Consistent function renderFullPage(html, initialState) { return ` <!DOCTYPE html>
... <div id=“root”>${html}</div> ... ` } Browser document.addEventListener('DOMContentLoaded', () => { render(<App />, document.getElementById('root')); }); <div id=“root”></div> <div id=“root”>${html}</div> document.getElementById('root')
Demo https://src.coding.net/