-
|
I am using HTMX to pull server-side rendered components into the browser, and I want to use Alpine.js to provide client-side reactivity for each loaded component. Online, the combination of these two frameworks is heavily advocated for. Real practice of it, however, is not so simple. I’ve found that when components are loaded dynamically (via Specifically:
Below is a breakdown of each scenario, what works, and what doesn't. Scenario 1 (Inline
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
It's also worth noting that the two scenarios that I have not found success with can work if I create the definition of the components upon initial page load, but, that defeats the whole purpose of pulling things in asynchronously via htmx. My primary goal would be that I can bring in both the definition of a component and the html using that component at the same time via an htmx call and Alpine initializes it on the spot. |
Beta Was this translation helpful? Give feedback.
-
|
#2605 Seems to have an answer that works for my use case. The problem was I was approaching things as if Alpine only initialized components one time at Alpine.start() and never tried again. However, after attempting the answer outlined in this comment, it seems that the issue is that HTMX has not fully loaded into the DOM before Alpine attempts to initialize it, resulting in Alpine components failing. I can only assume that inline x-data was working more smoothly because Alpine was able to recognize it right away when htmx brought it in, whereas it needed more time to recognize the other two methods (Alpine.data() and/or function calls) |
Beta Was this translation helpful? Give feedback.
#2605 Seems to have an answer that works for my use case.
The problem was I was approaching things as if Alpine only initialized components one time at Alpine.start() and never tried again. However, after attempting the answer outlined in this comment, it seems that the issue is that HTMX has not fully loaded into the DOM before Alpine attempts to initialize it, resulting in Alpine components failing.
I can only assume that inline x-data was working more smoothly because Alpine was able to recognize it right away when htmx brought it in, whereas it needed more time to recognize the other two methods (Alpine.data() and/or function calls)