phlex icon indicating copy to clipboard operation
phlex copied to clipboard

Disallow rendering the same component more than once

Open joeldrapper opened this issue 2 years ago • 1 comments

Rendering a component more than once can result in unintended consequences. For example, if your component uses DeferredRender to capture slots, the content of those slots will leak between different renders of the same instance.

I think we should update Phlex to disallow this, raising an error if you attempt to render the same instance of a component more than once. The place to do this would be in __final_call__ which is the method that gets renamed to call after the first render for caching.

https://github.com/phlex-ruby/phlex/blob/6a0eb85d2fe6e3959644c45185150ee29856eee0/lib/phlex/sgml.rb#L97-L127

I would approach this by first checking for an instance variable and raising if it's true, and then setting that instance variable to true.

joeldrapper avatar Nov 28 '23 18:11 joeldrapper

We can add this as a warning in 1.x.

joeldrapper avatar Jan 29 '24 15:01 joeldrapper

I will do this in a few days

bugdimes avatar Jun 14 '24 01:06 bugdimes