harmon25

harmon25

Happy Almost New Year everyone!

I made a library that integrates Surface + LiveView nicely with React.
I have aptly named it ReactSurface :stuck_out_tongue:

There are some other libraries that do this, or similar (phoenix_live_react).

ReactSurface is different in that it is a first class Surface component, and relies on the surface templating and component engine to work (does not rely on phoenix content_tag/3). Another difference is that it does not create sibling DOM nodes - it creates a nested react container, inside a live_view tracked container. keeping this data and context a bit more contained in the DOM. (see the README for an example)

ReactSurface uses react hydration(ReactDOM.hydrate) to keep the react internal component state intact between updates. It also exposes the live_view js functions (handleEvent, pushEvent, pushEventTo) to all components rendered in the react tree via a React Context accessible via a react hook: useLiveContext

Check out the demo app at ./demo to see it in action!

Motivation

Been playing with LiveView + Surface, and they are great - but not sure I can convince my employer to go all in on it.. This is a bit of a compromise to allow using React components for UI heavy bits of the page - but keeping the main layout, routing and data layer a server side concern.

Future ideas

  • Use a Surface Macro component to perform a server render with some default props at compile time.
    • This would provide compile time server side React rendering - but only for a default state and allow for the initial mounted phx hook to perform a ReactDOM.hydrate on the server rendered content.
    • the benefit of server side react rendering, without the complexity of stringing together a node server/stdio API to perform initial rendering of client side react components in production.
    • I might have the idea of a Surface Macro component wrong. @msaraiva would this work how I am imagining?
  • Dynamic loading of react components - ReactSurface does not rely on your React components existing on the window to work - the buildHook function takes a parameter where you supply a map of your components.

Feedback + ideas are much appreciated!
I do not suggest using this in prod just yet - more of a beta at the moment.

Will publish to hex once I have some tests in place.

Thanks for for checking it out! :smiley:

Where Next? Top

Trending in Announcing Top

woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. https://github.com/woylie/doggo Features Unstyled Phoenix components....
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Other Trending Topics Top

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
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews