question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

initialize vs connect

See original GitHub issue

How do the initialize() and connect() differs in use?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

36reactions
sstephensoncommented, Jan 29, 2018

Stimulus calls initialize() once per controller, and connect() every time the controller is connected to the DOM.

For example, say you have:

<div id="hello" data-controller="hello"></div>

Stimulus calls the corresponding controller’s initialize() method first, followed by connect().

If you then remove the element from the document:

const element = document.getElementById("hello")
element.remove()

Stimulus calls the controller’s disconnect() method.

Now, if you add the element back to the document:

document.body.appendChild(element)

Stimulus calls the controller’s connect() method again, but does not call initialize().

9reactions
sstephensoncommented, Jan 29, 2018

Yes, I agree! I’m working on getting it into the handbook right now 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lifecycle Callbacks - Stimulus Reference
Special methods called lifecycle callbacks allow you to respond whenever a controller or certain targets connects to and disconnects from the document.
Read more >
Stimulus.js Lifecycle Callbacks - Medium
Use initialize () . One might suggest that you use connect() but just ensure that the events are removed on disconnect() . But...
Read more >
Initialize, Connected, Disconnected - Stimulus - CodePen
URL Extension Required. When linking another Pen as a resource, make sure you use a URL Extension of the type of code you...
Read more >
Initialize and Connect to H2O — h2o.init • h2o
(Optional) A logical value indicating whether to try to start H2O from R if no connection with H2O is detected. This is only...
Read more >
When to initialize mysql connections in node - Stack Overflow
If you are going to pool connections, then don't initialize connections right when they're needed. When not using a pool, you can just...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found