Skip to content
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

useTransition: should I put it in initialize() or connect() ? #500

Open
nflorentin opened this issue Feb 28, 2024 · 1 comment
Open

useTransition: should I put it in initialize() or connect() ? #500

nflorentin opened this issue Feb 28, 2024 · 1 comment

Comments

@nflorentin
Copy link

nflorentin commented Feb 28, 2024

Hi,

The documentation show the use of useTransition in the connect function of the stimulus controller.

Doing that, I noticed that the enter/leave/toggleTransition callbacks pill up when the stimulus controller is connected multiple times. Example:

  1. Page loads, controller is initialized and connected
  2. Request made with turbo, I call disconnect() and connect() from my controller because I detect a morph
  3. I have now 2 calls on enter/leave/toggleTransition on click on my element (so my dropdown is not working anymore)
  4. (3 calls on next request, ect)

It seems that useTransition does not support being connected multiple times.

I tried 2 things that seem to work but I don't know which one is better:

  1. keep calling useTransition from connect function and add the following lines in a disconnect function:
this.enter = null
this.leave = null
this.toggleTransition = null
  1. call useTransition in initialize function

My main goal is to avoid memory leak.

Thanks for your work! and your future help :-)

@gamecreature
Copy link

I had the same problem, drag/drop elements with a stimulus useTransition controller on it. Which results in disconnect/connect. Which makes the useTransaction fail the next time (double enter/leave events).

Added the disconnect sample from above, which solves the problem. (Thanks @nflorentin)

Though I don't think this is a clean solution, maybe add disconnectUseTransition to the library and documentation? (At the moment you need to have the knowledge that enter/leave and toggleTransition properties are being used).
But.. it's still beta, so I guess improvements are coming ☺️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants