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

The order of the lifecycle callbacks is confusing Stimulus 3 #471

Closed
sandergroen opened this issue Oct 11, 2021 · 3 comments · Fixed by #473
Closed

The order of the lifecycle callbacks is confusing Stimulus 3 #471

sandergroen opened this issue Oct 11, 2021 · 3 comments · Fixed by #473

Comments

@sandergroen
Copy link

In the documentation the following callbacks are defined:

initialize() | Once, when the controller is first instantiated
connect() | Anytime the controller is connected to the DOM
[name]TargetConnected(target: Element) | Anytime a target is connected to the DOM
disconnect() | Anytime the controller is disconnected from the DOM
[name]TargetDisconnected(target: Element) | Anytime a target is disconnected from the DOM

And in my case I use the connect() and [name]TargetConnected(target: Element) callbacks. I would expect the connect callback to be called first but to my surprise the [name]TargetConnected callbacks are being called before the connect callback. I would expect them to be called in the same order as the order in the documentation.

In my dom I have something like this:

  <div data-controller="names">
    <div data-names-target="name">...</div>
    <div data-my-controller-target="name">...</div>
  </div>
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ['name']
  connect() {
    console.log('connecting names controller')
  }

  nameTargetConnected() {
    console.log('connecting name target')
  }
}

Console output:

names_controller.js:10 connecting name target
names_controller.js:10 connecting name target
names_controller.js:6 connecting names controller
@dhh
Copy link
Member

dhh commented Oct 11, 2021

cc @seanpdoyle

seanpdoyle added a commit to seanpdoyle/stimulus that referenced this issue Oct 11, 2021
Closes hotwired#471

---

This commit re-arranges the documentation for Controller Lifecycle
Callbacks to reflect the order of execution and elaborates with more
details when possible.

The possibilities for the callback execution order was discussed and the
[current order was agreed upon][order].

[order]: hotwired#367
@seanpdoyle
Copy link
Contributor

I've opened #473 to improve the documentation around the lifecycle execution order.

@dhh dhh closed this as completed in #473 Oct 11, 2021
dhh pushed a commit that referenced this issue Oct 11, 2021
Closes #471

---

This commit re-arranges the documentation for Controller Lifecycle
Callbacks to reflect the order of execution and elaborates with more
details when possible.

The possibilities for the callback execution order was discussed and the
[current order was agreed upon][order].

[order]: #367
@sandergroen
Copy link
Author

Thank you @dhh and @seanpdoyle!

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

Successfully merging a pull request may close this issue.

3 participants