Want to export Component for server side rendering in ReactJS? Using a factory allows you to have all your components in separate files and instantiate them without using jsx syntax in your server. Very useful for the main wrapper component.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | require('babel-core/register')({ presets: ['react'] }); var express = require('express'); var reactDOM = require('react-dom/server'); var react = require('react'); var app = express(); app.get('/', function (req, res) { var mainFile = require('./app.jsx'); var output = reactDOM.renderToString(react.createFactory(mainFile)({ data: yourInitialData })); res.send(output); }); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.