Pre-render x-for entries #4415
JuroOravec
started this conversation in
3. Feature Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Idea
Pre-render items for
x-for
, so lists managed by Alpine don't have to start empty.Problem
I'm using AlpineJS with server-side rendering (Django). When I'm rendering lists that are not intended to change size, I can render them statically (the list is rendered as HTML).
When I want to allow users to modify a list in the UI, I delegate to AlpineJS to manage that. So I use Alpine's
x-for
to render out the list items.But the problem is that when I use AlpineJS, there's that jitter on page load, when Alpine has not been loaded yet and the list is empty.
So I'd like a way to "pre-populate" a list. So that I could render the initial list of items on server-side, and then, once Alpine has been loaded, it would take over.
Workaround
The current workaround looks something like this:
The items are rendered server-side, and each item contains
data-x-item
that describes the Alpine-side list item that should be used to render the same content.Then, when the component is initialized, in
init()
, I extract the items from the pre-rendered items, and use that data to render a new list, which is now fully managed by AlpineJS (and thus allows users to add / remove items.Once the new list is prepared, the old (server-side rendered) list can be removed.
Beta Was this translation helpful? Give feedback.
All reactions