To use phoenix_live_react, you must add it to your Elixir dependencies and connect the JavaScript hooks to your LiveView socket.
1. Add Elixir Dependency
Add {:phoenix_live_react, "~> 0.5"} to your mix.exs file and run mix deps.get.
2. Connect JavaScript Hooks
In your app.js, import LiveReact and initLiveReact, then include LiveReact in your LiveSocket hooks configuration. You can optionally call initLiveReact() on DOMContentLoaded to speed up initial rendering (note: pushEvent, pushEventTo, and handleEvent props are not available during this initial client-side render).
3. Import Helper in Elixir
Import PhoenixLiveReact in your web module (e.g., lib/MyAppWeb.ex) within the live_view quote block to make the component helpers available to your views.
# mix.exs
def deps do
[
{:phoenix_live_react, "~> 0.5"}
]
end
// app.js
import LiveReact, { initLiveReact } from "phoenix_live_react"
let hooks = { LiveReact }
let liveSocket = new LiveSocket("/live", Socket, { hooks, params: { _csrf_token: csrfToken } })
// Optional: render on page load for speed
document.addEventListener("DOMContentLoaded", e => {
initLiveReact()
})
# lib/MyAppWeb.ex
def live_view do
quote do
import PhoenixLiveReact
end
end