ux icon indicating copy to clipboard operation
ux copied to clipboard

Error: The data-live-preserve attribute requires an id attribute

Open kachnitel opened this issue 1 year ago • 7 comments

Using an id prop on a nested LiveComponent causes an error when child model is updated from parent.

Error:

Error: The data-live-preserve attribute requires an id attribute to be set on the element at live_controller.js:1353:1

How to reproduce

See https://github.com/kachnitel/symfony-live-component-bug/commit/9d28445789bf758be0710059e5549001f8ead3b0

  • clone repo and install dependencies
  • start server and go to homepage
  • click the (small and empty) button to call an action in parent component to update data model
  • witness error

kachnitel avatar Mar 22 '24 03:03 kachnitel

LiveComponent used data-id attributes before, but to follow idiomorph usage and more generally the hotwire (Stimulus, Turbo) suite, LiveComponent uses "id" attributes since 2.15 (see UPGRADE.md there).

So you should avoid setting it yourself for nested / child component i guess... did you try to pass "key" instead of "id" ?

smnandre avatar Mar 24 '24 22:03 smnandre

Yep using key instead of id solves the issue. I used the id attribute primarily based on this section of documentation, having been unsure if there's any internal "magic" watching the ID that wouldn't be covered by "key". That said, I've used "key" everywhere I needed an id and it works as expected. Suppose it's just a matter of documentation update then?

kachnitel avatar Mar 25 '24 06:03 kachnitel

I'm glad if that works for you 😃

The documentation does require an update indeed... would you like to open a PR ?

smnandre avatar Mar 25 '24 07:03 smnandre

Haha I'm the worst at docs so that's a good motivation to give it a shot! I should be able to catch some breath a bit tomorrow (pending client meeting anyways) so I'll tackle have a go at it.

kachnitel avatar Mar 27 '24 08:03 kachnitel