hurricanenate

hurricanenate

My company is taking its first steps in integrating an existing React SPA into a brand-new Phoenix app. We’ve set up auth using phoenix.gen.auth, and are looking at the first case where we need Phoenix-generated data in our UI layer: adding a logout link to our header.

This is how the link is generated in _user_menu.html.heex:

<li><%= link "Log out", to: Routes.user_session_path(@conn, :delete), method: :delete %></li>

What’s the best way to recreate this in our React app?

The first approach I came up with was to add a template that puts any data we might need into a <script></script> tag and attaches it to the window object so that our React components could grab it. This is how I implemented it:

root.html.heex:

<head>
  ...
  <%= render "_script_data.html", assigns %>
  ...
</head>

_script_data.html.heex:

<script type="text/javascript">
  window.dashboardAppData = {
    currentUser: {
      <%= if @current_user do %>
        email: '<%= @current_user.email %>',
      <% end %>
    },
    csrfToken: document.querySelector("meta[name='csrf-token']").getAttribute("content"),
    routes: {
      userSessionDelete: '<%= Routes.user_session_path(@conn, :delete) %>',
      ...
    }
  }
</script>

My React component, HeaderToolbar.js:

function HeaderToolbar() {
  const appData = window?.dashboardAppData;

  return (
    {appData && (
      <a
        href={appData.routes.userSessionDelete}
        data-to={appData.routes.userSessionDelete}
        data-method="delete"
        data-csrf={appData.csrfToken}
      >
        Log Out
      </a>
    }
  )
}

This approach seems workable, but far from ideal: I don’t love the step of creating a giant object of data and making decisions each time about naming conventions. I also lose the utility of the <%= link ... %> tag, as I’m forced to recreate what a Phoenix link looks like, mirroring the necessary data attributes first by rendering it normally in a template and then recreating it in my React component.

Does anyone have any suggestions for better ways to approach this integration? Whatever choices we make at this point will determine the pattern for how we do quite a bit of other work and I’d like to find the best option possible.

Showing Posts 1 to 10

tomkonidas

tomkonidas

There is a chapter at the end of Real-Time Phoenix that goes into integrating with other front ends using channels.

There is specifically a section Exploring Front-End Technologies → Single-Page Apps with React

hurricanenate

hurricanenate OP

Thanks a lot for that tip—I’ve just purchased the book and will check it out ASAP.

hurricanenate

hurricanenate OP

One question: it looks like the book deals with integrating with Phoenix channels, which I understand to be open connections that let you receive data as it is updated or changed directly from the server. Does it deal at all with the question of how to integrate the rendering layer, as in my example with the log out button in the OP? Right now I’m most curious about how to approach the rendering layer of views in React components.

kokolegorille

kokolegorille

There is a tool to help rendering React with Elixir.

I am curious about the previous backend, and how it was working before.

tomkonidas

tomkonidas

I would think so. Looking fast at the book on page 280.

Well-factored components can be difficult to grasp without a concrete example to show the way. We’ll be working on a Phoenix application with a single-page React front end. You’ll see examples of container components, presentation components, and how to wire them together with contexts and React Router. There will be a bit of React-specific patterns that you may not be used to, but don’t worry too much if you’re not familiar with React.

I assume the presentation components are the ones you are interested in. It looks like he is using normal React Router. You would probably still need to do some sort of JSON API/GraphQL/Channels call in the route component I would guess. This also would depend if you are only using React, or just sprinkling it in a Phoenix app. The way he shows in the book I believe would be using React as the only front end and then using Channels to talk to the Phoenix backend.

joaoevangelista

joaoevangelista

hi!
idk if I understood correctly, but you want to pass to the react components the result of Routes.user_session_path(@conn, :delete) so it can know what url to call to log out?

Is the react app being embedded in a phoenix template? If so, you could assign the routes to an object at the window, as you did, or maybe as a prop to your main react component ?

hurricanenate

hurricanenate OP

Yeah, that approach definitely works. I was hoping for a more integrated solution that doesn’t require those kinds of explicit pass-throughs.

hurricanenate

hurricanenate OP

This approach may make the most sense: my colleague was recommending we try an SSR-like approach using Phoenix’s render_to_string method, and it looks like ReactRender basically is a more fully realized version of that approach: I’ll be giving that a try today.

I am curious about the previous backend, and how it was working before.

The SPA also interacts with a separate go app via rest endpoints, and will continue to do so. We’re adding the Phoenix app as a way of giving this app the ability to handle its own separate, user-related concerns, which is why adding authentication is our first step.

kokolegorille

kokolegorille

This is how I would do with a Phoenix Backend, either REST or GraphQL, with Absinthe.

But I don’t see how You want to procede… using phx.gen.auth is not done for API.

And SPA have their own router, their own authentication, usually with a Phoenix Token.

Unless You want to use Phoenix, with React as the View layer.

You can pass data from backend to frontend via data attributes, even json encoded attributes, that You could parse JS side.

You can use Hooks if You use liveview, as a way to interact with JS.

bpaulino0

bpaulino0

Probably very late on the topic, but back in 2022, I wrote a blogpost on how to integrate the entire React pipeline within Phoenix so you can have a decent developer experience, but also a reasonable deployment pipeline that does not compromise on your current Elixir/Phoenix build steps.

I think the most challenging part is to make sure that you have a up2date build step for your SPA app and at the same time you don’t change your Phoenix setup too much. And you only need NodeJS during build-time.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews