Weird behavior where partial is getting rendered instead of component #1708
-
Hey 👋🏻 I'm seeing something so weird that it is making me question my sanity! It goes like this: I had a partial file that was linked to a Stimulus controller and would toggle a spinner/loader (however you want to call it) while a Turbo request it being made, something like this:
<%= content_tag(:div, data: { controller: "loader" }, class: "#{extra_classes}") do %>
<%= content_tag(:div, role: "status", class: "hidden", data: { loader_target: "loader"}) do %>
<div class="animate-spin inline-block w-10 h-10 border-[3px] border-current border-t-transparent dark:text-red-700 text-blue-700 rounded-full" role="status" aria-label="loading">
<span class="sr-only">Loading...</span>
</div>
<% end %>
<% end %>
export default class extends Controller {
static targets = ["loader"];
connect() {
document.addEventListener("turbo:submit-start", this.toggleSpinner.bind(this));
document.addEventListener("turbo:submit-end", this.toggleSpinner.bind(this));
}
toggleSpinner() {
toggle(this.loaderTarget);
}
} so whenever I want a spinner, I just do this was working perfectly fine but I wanted to turn this partial into a viewcomponent, so I simply created a
# typed: true
# frozen_string_literal: true
class LoaderComponent < ApplicationComponent
def initialize(extra_classes: nil)
@extra_classes = extra_classes.nil? ? "mx-auto" : extra_classes
end
private
attr_reader :extra_classes
end and also updated the call from I added some logging on the Stimulus controller, maybe the Stimulus is not doing what it is supposed to do: connect() {
console.log(this.loaderTarget);
document.addEventListener("turbo:submit-start", this.toggleSpinner.bind(this));
document.addEventListener("turbo:submit-end", this.toggleSpinner.bind(this));
console.log("Binded");
}
toggleSpinner() {
toggle(this.loaderTarget);
console.log(this.loaderTarget);
} and this is what I see in the console so it is
I couldn't get this to show to save my life! After a lot of back and forth, I ended up reverting the removal of the partial I showed in the beginning, and to my surprise, the spinner is now showing up but it is mind-boggling: a. updating the two top it looks like the two outer divs are coming from the component and the inner div is coming from the partial! this doesn't make any sense. really, I have never seen this and I'm out of ideas and about to give up. tried everything possible: clear cache, incognito window, update gems, js deps, delete and redo everything am I missing something that is so under my nose that I can't see? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @luizkowalski! Can you provide a minimal repo where we can reproduce the most basic version of the problem? |
Beta Was this translation helpful? Give feedback.
Hi @luizkowalski! Can you provide a minimal repo where we can reproduce the most basic version of the problem?