DEV Community

loading...

self-referential data-reflex-root

Scott Barrow
VP Engineering at Ollie Order
Updated on ・1 min read

It can sometimes be tricky to re-render an element, partial or view component in a selector morph from a reflex, especially if it needs new.

An alternative approach to selector morphs is wrapping your element(s) in a div with data-reflex-root. The value of the data attr will be used as the selector(s) for the morph.
A neat trick is that you can do this on itself

<div id="unique-morph-target" data-controller='resource-toggle-component' data-reflex-root='#unique-morph-target'>
Enter fullscreen mode Exit fullscreen mode

N.b. only the children of this element will be morphed, and not the element itself!
One major difference is that this approach still hits the controllers original action, which may or may not be desirable.
There’s nothing special needed in the reflex
ref: https://docs.stimulusreflex.com/morph-modes#scoping-page-morphs

Discussion (0)