-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Phoenix LiveView & React integration #2988
Open
zeorin
wants to merge
20
commits into
main
Choose a base branch
from
2850-migrate-workflow-edit-to-react
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add6549
to
aeab027
Compare
44ba0e5
to
cf20f0a
Compare
cf20f0a
to
a641536
Compare
Associates a JSX file with a component. Such files will become entrypoints for ESBuild, but common code is code-split by ESBuild into separate chunks.
After the Monaco refactor, the dev server needs to know what to do with these files.
a641536
to
f472c01
Compare
We've got a bit of an issue with using runtime during production builds - since runtime.exs expects pretty much every env variable to be available; which isn't realistic during build time. This means that the React build tooling (the new macro work on `React`) won't work right now with the automatic entrypoint generator - perhaps we just go the manual route for now.
11 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR adds more idiomatic integration between Phoenix LiveView and React.
Phoenix LiveView and React Components can be interleaved arbitrarily, and they will still participate in their respective lifecycles, however every component that is a child of a React component (even indirectly) will not have diffs sent over the channel on changes. Rendering is still done using patches, but the actual update payload is not diffed. (Note that I think it is possible to solve this but that would be more effort than using targeted portals, as below.)
React components can be mount as a portal in another React component. This way you can have a React Component tree that does not necessarily have to match the DOM tree. This is handy for sharing e.g. context with multiple React components (and also means that you can avoid interleaving Phoenix LiveView and React Components if you are concerned about the over-the-wire perf impact of doing so). Ids are used to associate portals and their targets. These can be nested arbitrarily deeply.
phx-*
events Just Work™ because Phoenix LiveView uses event delegation to listen to them.There's a
jsx
macro that you can use to create a Phoenix functional component. It will create a functional component with the name of the file, so if you use e.g.jsx("assets/js/react/components/Foo.tsx")
, you can then use<.Foo>
in an Heex template. You can also annotate the macro call withattr
andslot
like a regular component. The:inner_block
slot is automatically translated to thechildren
prop.React components included in this way will be included as their own entrypoint by ESBuild, but code that they share dependencies on with the rest of the app will be code-split into separate chunks.
This lays the some of the groundwork for #2850.
Still to be done:
Validation steps
Check out the branch, update deps, and start dev server. Navigate to
http://localhost:4000/dev/react
to see some demo componentsAdditional notes for the reviewer
There are still a bunch of
TODO
comments in the code that need review.This work has taken inspiration from:
live_react
AI Usage
Please disclose how you've used AI in this work (it's cool, we just want to know!):
You can read more details in our Responsible AI Policy
Pre-submission checklist
:owner
,:admin
,:editor
,:viewer
)