Skip to content

Conversation

@LostKobrakai
Copy link
Contributor

No description provided.

@LostKobrakai LostKobrakai marked this pull request as draft July 7, 2025 18:41
@SteffenDE
Copy link
Collaborator

To have this make a difference, we need to also think about improving the :let situation, since it introduces a variable that is not change tracked. So currently adding keys here does not improve diffing :(

~H"""
<input
:for={{name, value, index} <- @hidden_inputs}
:key={"#{name}_#{index}"}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using index as part of the key is almost certainly a bug waiting to happen.

I'm assuming you're avoiding using just name as key since that might not be unique, but adding index doesn't fix this, as it just leads to different kind of bugs when name isn't unique.
I've seen this many times in react, those bugs are very hard to find.

(I haven't properly studied this PR or the implementation of :key so ignore me if you definitely know better. Just something that jumped out to me)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Names are indeed not unique. myinput[] is a totally valid name to be used multiple times.

But I'm curious what you think the "bugs waiting to happen" would be. Keying is an optimization, which in the worst case would only fall back to being unoptimized like inputs_for is right now already.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how :key is implemented in 1.1, but for react you introduce bugs when key is not uniquely identifying the same element between renders. I would be very surprised if the implementation of liveview avoids these issues completely.

The bugs are subtle and only happen when you reorder or add/remove elements. Here's an old article for react: https://robinpokorny.com/blog/index-as-a-key-is-an-anti-pattern/
example from the blog: https://jsbin.com/wohima/edit?output

For the concrete code here it's certainly harder to run into this issue, but if it's possible in principle I guarantee a user will run into this issue by accident, and then tracking it down will be almost impossible.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants