Combining react and jade Combining react and jade reactjs reactjs

Combining react and jade


Although this may be a tiny bit off topic: We stuck with jade templates.

Basically we wanted the flexibility to use a non-react + flux architecture for areas of the site when and if that need arose. Our site is basically made up of a number of smaller SP apps: Site, UserAccount, Team and Admin.

Why did we do this?

  • Smaller filesize and overhead for users who are not accessing all sections of the site.

  • Option to "opt out" of React and flux if and when the need arises.

  • Simpler, server side authentication.


The way we have done it successfully was to render a JSX shell template (Html.jsx) on the server using React.renderToStaticMarkup() and then send it as the response to every server-side express route request that is meant to deliver some HTML to the browser. Html.jsx is just a shell containing html head information and GA scripts etc. It should contain no layout.

// Html.jsxrender(){  return (    <html>      <head>        // etc.      </head>      <body>        <div          id="app"          dangerouslySetInnerHTML={{__html: this.props.markup}}>        </div>      </body>      <script dangerouslySetInnerHTML={{__html: this.props.state}</script>      <script>        // GA Scripts etc.      </script>    </html>  )}

Remember it is totally fine and even recommended to use dangerouslySetInnerHTML on the server when hydrating your app.

Dynamic layout should be done with your your isomorphic components through a hierarchy of components based on their state/props configuration. If you happen to be using React Router, then your router will render view handlers based on the routes you provide it so that means you don't need to manage that yourself.

The reason we use this technique is to architecturally separate our "App" which is isomorphic and responds to state from our server-side template shell which is just a delivery mechanism and is effectively boiler plate. We even keep the Html.jsx template amongst all the express components within our app and do not let it mix with the other isomorphic React components.

One of the most helpful resources I found for working out React/isomorphic architecture was https://github.com/yahoo/flux-examples/tree/master/react-router which is where we stole this technique from.

We explored the idea of integrating handlebars as a templating engine for client's devs using our products in the future but decided that it was less complex to write our own DSL in JSX and use some simple parsing routines to parse our HTML-like DSL to JSX by adding things like export default (ES6 module syntax) at the start of the template and then import the template to a rendering component.

You could of course follow that line of thought and use a jade compiler to spit out the template and then add module syntax around that if you think separate jade files are essential. I also noticed this project as well although I have not explored it in anger: https://github.com/jadejs/react-jade.