defmodule ReactRender do use GenServer @moduledoc """ A genserver that controls the starting of the node render service """ @doc """ Starts the ReactRender and underlying node react render service `render_server_path` is the path to the react render service relative to your current working directory """ @spec start_link(binary()) :: {:ok, pid} | {:error, any()} def start_link(render_server_path) do GenServer.start_link(__MODULE__, [render_server_path], name: __MODULE__) end @doc """ Stops the ReactRender and underlying node react render service """ @spec stop() :: :ok def stop() do GenServer.stop(__MODULE__) end @doc """ Given the `component_path` and `props`, returns html. `component_path` is the path to your react component module relative to the render service. `props` is a map of props given to the component. Must be able to turn into json """ @spec get_html(binary(), map()) :: {:ok, binary()} | {:error, map()} def get_html(component_path, props \\ %{}) do case do_get_html(component_path, props) do {:error, _} = error -> error {:ok, %{"markup" => markup}} -> {:ok, markup} end end @doc """ Same as `get_html/2` but wraps html in a div which is used to hydrate react component on client side. This is the preferred function when using with Phoenix `component_path` is the path to your react component module relative to the render service. `props` is a map of props given to the component. Must be able to turn into json """ @spec render(binary(), map()) :: {:ok, binary()} | {:error, map()} def render(component_path, props \\ %{}) do case do_get_html(component_path, props) do {:error, _} = error -> error {:ok, %{"markup" => markup, "component" => component}} -> props = props |> Jason.encode!() |> String.replace("\"", """) html = """