Accepted answer

for a given website / web-application, you can use react either client-side, server-side or both.


over here, you are completely running reactjs on the browser. this is the simplest setup and includes most examples (including the ones on the initial html rendered by the server is a placeholder and the entire ui is rendered in the browser once all your scripts load.


think of reactjs as a server-side templating engine here (like jade, handlebars, etc...). the html rendered by the server contains the ui as it should be and you do not wait for any scripts to load. your page can be indexed by a search engine (if one does not execute any javascript).

since the ui is rendered on the server, none of your event handlers would work and there's no interactivity (you have a static page).


here, the initial render is on the server. hence, the html received by the browser has the ui as it should be. once the scripts are loaded, the virtual dom is re-rendered once again to set up your components' event handlers.

over here, you need to make sure that you re-render the exact same virtual dom (root reactjs component) with the same props that you used to render on the server. otherwise, reactjs will complain that the server-side and client-side virtual doms don't match.

since reactjs diffs the virtual doms between re-renders, the real dom is not mutated. only the event handlers are bound to the real dom elements.


is it 2 separate ways to build the application, or can they be used together?

they can be used together.

if we can use it together, how to do it - do we need to duplicate the same elements on the server side and client side? or, can we just build the static parts of our application on the server, and the dynamic parts on the client side, without any connection to the server side that was already pre-rendered?

it's better to have the same layout being rendered to avoid reflow and repaint operations, less flicker / blinks, your page will be smoother. however, it's not a limitation. you could very well cache the ssr html (something electrode does to cut down response time) / send a static html which gets overwritten by the csr (client side render).

if you're just starting with ssr, i would recommend start simple, ssr can get very complex very quickly. to build html on the server means losing access to objects like window, document (you have these on the client), losing ability to incorporate async operations (out of the box), and generally lots of code edits to get your code ssr compatible (since you'll have to use webpack to pack your bundle.js). things like css imports, require vs import suddenly start biting you (this is not the case in default react app without webpack).

the general pattern of ssr looks like this. an express server serving requests:

const app = express();
const port = 8092;

// this is fired every time the server side receives a request
function handlerender(req, res) {
    const fullurl = req.protocol + '://' + req.get('host') + req.originalurl;
    console.log('fullurl: ', fullurl);
    console.log('req.url: ', req.url);

    // create a new redux store instance
    const store = createstore(reducerfn);

    const urltorender = req.url;
    // render the component to a string
    const html = rendertostring(
        <provider store={store}>
            <staticrouter location={urltorender} context={{}}>
    const helmet = helmet.renderstatic();

    // grab the initial state from our redux store
    const preloadedstate = store.getstate();

    // send the rendered page back to the client
    res.send(renderfullpage(helmet, html, preloadedstate));

my suggestion to folks starting out with ssr would be to serve out static html. you can get the static html by running the csr spa app:


don't forget, the only reasons to use ssr should be:

  1. seo
  2. faster loads (i would discount this)

hack :


i was actually wondering the same researching quite a bit and while the answer you are looking for was given in the comments but i feel it should be more prominent hence i'm writing this post (which i will update once i can come up with a better way as i find the solution architecturally at least questionable).

you would need to write your components with both ways in mind thus basically putting if switches everywhere to determine whether you are on the client or the server and then do either as db query (or whatever appropriate on the server) or a rest call (on the client). then you would have to write endpoints which generate your data and expose it to the client and there you go.

again, happy to learn about a cleaner solution.


image source: walmart labs engineering blog



nb: ssr (server side rendering), csr (client side rendering).

the main difference being that with ssr, the servers response to the clients browser, includes the html of the page to be rendered. it is also important to note that although, with ssr, the page renders quicker. the page will not be ready for user interaction until js files have been downloaded and the browser has executed react.

one downside is that the ssr ttfb (time to first byte) can be slightly longer. understandably so, because the server takes some time creating the html document, which in turn increases the servers response size.

Related Query

More Query from same tag