Last active
August 29, 2015 14:24
-
-
Save vesparny/f0d2732b075bba05a192 to your computer and use it in GitHub Desktop.
React.js post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const flux = new Flux(); | |
const router = Router.create({ | |
routes: routes, | |
location: Router.HistoryLocation | |
}); | |
router.run((Handler, state) => { | |
React.render( | |
<Handler {...state} />, | |
document.getElementById('root')); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body | |
#root!=appString | |
script(src='#{js}', defer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let MyComponent = React.createClass({ | |
render() { | |
return <div>Hi from a component!</div> | |
} | |
}); | |
React.render(<MyComponent />, document.getElementById('root')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let htmlString = React.renderToString(<MyComponent />); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (process.env.NODE_ENV === 'production') { | |
const webpackBuildStats = require('../build/webpackBuildStats'); | |
js = `/build/bundle-${webpackBuildStats.hash}.min.js`; | |
} | |
export default (req, res, next) => { | |
const flux = new Flux(); | |
let appString; | |
const router = Router.create({ | |
routes: routes, | |
location: req.path | |
}); | |
router.run((Handler, state) => { | |
const isNotFound = state.routes.some(route => route.name === 'not-found'); | |
res.status(isNotFound ? 404 : 200); | |
try { | |
let appString = React.renderToString( | |
<FluxComponent flux={flux}> | |
<Handler {...state} /> | |
</FluxComponent> | |
); | |
} catch (err) { | |
// In case of errors render an Error Page | |
appString = React.renderToString(<ErrorPage />); | |
res.status(500); | |
return res.render('react/server/views/error', { | |
appString: appString | |
}); | |
} | |
res.render('react/server/views/index', { | |
js: js, | |
appString: appString | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment