harmon25

harmon25

ReactSurface - Render React Components via Surface

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?

Popular in Libraries Top

OvermindDL1
Been making an MLElixir thing (not released yet…) for fun in spare time in the past day. I’m just trying to see how much I can get an ML...
132 13487 106
New
cjen07
parameterized pipe in elixir: |n> edit: negative index in |n> and mixed usage with |> are supported example: use ParamP...
New
woutdp
Hi! I wanted to introduce my latest project LiveSvelte. It allows you to render Svelte inside LiveView with end-to-end reactivity. It’s ...
New
benlime
I created a new library GitHub - benvp/ex_cva: Class Variance Authority for Elixir which aims to make it very easy to define different va...
New
tfwright
After working on it for a couple of months and using it in production for most of that time, today I’ve released LiveAdmin, a LiveView ba...
New
martinthenth
Hello everybody :wave: Recently, some of my colleagues talked about database ids and uuids and their problems, and I remembered the pain...
New
mbuhot
EctoJob A transactional job queue built with Ecto, PostgreSQL and GenStage Available on Hex.pm: ecto_job | Hex Docs: API Reference — ec...
New
gabrielpoca
Hello everyone! I want to share with you something that I’m really proud of: https://stillstatic.io/ Still is a static site builder for...
New
Qqwy
While not as prevalent as in imperative languages, arrays (collections with efficient random element access) are still very useful in Eli...
New
anshuman23
Hello all, I have been working on my proposed project called Tensorflex as part of Google Summer of Code 2018.. Tensorflex can be used f...
New

Other popular topics Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

Sub Categories:

We're in Beta

About us Mission Statement