8000 [React] Add `permanent` option to `react_component` function by smnandre · Pull Request #2283 · symfony/ux · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[React] Add permanent option to react_component function #2283

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

Merged
merged 1 commit into from
Oct 21, 2024

Conversation

smnandre
Copy link
Member
@smnandre smnandre commented Oct 18, 2024
Q A
Bug fix? yes
New feature? yes
Issues Fix #...
License MIT

(Issue reported on a work project)

Using Turbo data-turbo-permanent creates race conditions that lead to the controller code failing to keep or reload the component.

In the current implementation, the Stimulus controller unmounts the component then tried to create it again.. which cannot be done:

Once you call root.unmount you cannot call root.render again on the same root. Attempting to call root.render on an unmounted root will throw a “Cannot update an unmounted root” error. However, you can create a new root for the same DOM node after the previous root for that node has been unmounted.

https://react.dev/reference/react-dom/client/createRoot#root-unmount


The proposed implementation uses a short delay before calling unmount. It is "morphing-library-agnostic", and will work with any Turbo-like framework, with no impact on the existing projects using UX React (except a bug fixed 😅 ).

New implementation:

A new option permanent prevent the "unmount" of the component when set to true

{# This component will not be unmounted on "disconnect" #}

{{ react_component('MyComponent', props, {permanent: true}) }}

This allows to avoid the bug when a component is disconnected and immediately reconnected (ex with Turbo)

@carsonbot carsonbot added Bug Bug Fix Feature New Feature React Status: Needs Review Needs to be reviewed labels Oct 18, 2024
@codisart
Copy link

Hello @smnandre, I work on a website for a french media company which include a audio player which is implemented using react. We tried to use symfony/ux react component with turbo and a data-turbo-permanent attribute to allow the non stop music playing during navigation.

We discovered that it was not possible with the current code like you said. But it was easily fix moving the connect function to the initialize function and removing the disconnect entierly.

Should we use your implementation instead ? Would we see bugs with our solution ?
It seems to me that using only initialize lifecycle is more coherent for a permanent component.

@smnandre smnandre changed the title [React] Support Turbo data-turbo-permanent [React] Add permanent option to react_component function Oct 20, 2024
@smnandre
Copy link
Member Author
smnandre commented Oct 20, 2024

Hi @codisart !

I changed the implementation to avoid playing with timeouts/delays.

(I updated the first message)

@smnandre smnandre requested review from Kocal, kbond and WebMamba October 20, 2024 12:09
@kbond kbond force-pushed the feat/turbo-permanent-compatibility branch from 6046811 to 4d60724 Compare October 21, 2024 19:05
@kbond
Copy link
Member
kbond commented Oct 21, 2024

Thanks Simon.

@kbond kbond merged commit b255914 into symfony:2.x Oct 21, 2024
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug Fix Feature New Feature React Status: Needs Review Needs to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0