Is there an idiomatic way for creating dependency injecting components?
For context, I am developing a UI library (not necessarily headless), and I want to give users the ability to customize how certain inner workings of components (e.g. things that normally would be incapsulated, such as inner elements of components) render.
And following are the examples.
Context-based approach
That's how Kobalte and Corvu do it. The biggest disadvantage for me is that it's not type safe and requires runtime assertions.
Injecting through properties
Hooks
I can't remember any real world examples but that's what it roughly should look like I guess...
The hook could return helper functions for some components as well.
For each of the following examples I want to ask a few questions:
a) Does it actually work flawlessly with Solid's reactivity?
b) How idiomatic is it?
c) Is it reliable to be used in production?
1 Reply
a. Yes
b. I would consider all options to be idiomatic
c. 💯
the biggest disadvantage for me is that it's not typesafeYe... it's a bummer that you cannot type the children of jsx components. I feel these type of
compound components
are a very established pattern though and communicate the intent well imo.