ReactRender

Build Status Hex.pm License: MIT Coverage Status

Renders React as HTML

Documentation

The docs can be found at https://hexdocs.pm/react_render.

Installation

def deps do
[
{:react_render, "~> 2.0.0"}
]
end

Getting Started with Phoenix

"react_render": "file:../deps/react_render"
npm install
const ReactRender = require('react_render/priv/server')
ReactRender.startServer()
render_service_path = "#{File.cwd!}/assets/js/server.js"
pool_size = 4
supervisor(ReactRender, [[render_service_path: render_service_path, pool_size: 4]])
import React, {Component, createElement} from 'react'
class HelloWorld extends Component {
render() {
const {name} = this.props
return <div>Hello {name}</div>
}
}
export default HelloWorld
def index(conn, _params) do
component_path = "#{File.cwd!}/assets/js/HelloWorld.js"
props = %{name: "Revelry"}
{ :safe, helloWorld } = ReactRender.render(component_path, props)
render(conn, "index.html", helloWorldComponent: helloWorld)
end

component_path can either be an absolute path or one relative to the render service. The stipulation is that components must be in the same path or a sub directory of the render service. This is so that the babel compiler will be able to compile it. The service will make sure that any changes you make are picked up. It does this by removing the component_path from node's require cache. If do not want this to happen, make sure to add NODE_ENV to your environment variables with the value production.

<%= raw @helloWorldComponent %>
import {hydrateClient} from 'react_render/priv/client'
import HelloWorld from './HelloWorld.js'
function getComponentFromStringName(stringName) {
// Map string component names to your react components here
if (stringName === 'HelloWorld') {
return HelloWorld
}
return null
}
hydrateClient(getComponentFromStringName)