Component that can render a different tag depending on prop?
I want to make a title that renders a different component depending on the
as
props you give it. I've written the following component, using a Switch
to render the correct component.
I am not sure if this is the best way to accomplish what I want. What are your opinions on the way I should have done this? Is this good practice, or is there a cleaner way? Thank you very much for your help and your time! š
10 Replies
I could also use a
Dynamic
component like this, but I don't know which is the best solution. š¤
This is the way
Is this because it's cleaner and Switch and Dynamic have the same impact? I'm just wondering if the Dynamic component has a bigger impact. The reason is that this Title will be used everywhere in my website, and I would rather not impact the performance too much. š
This title is not really dynamic, I just want to render a different heading level once and then leave it like this š
Dynamic certainly has the higher impact, but Solid's performance is so good that you'll hardly notice even with 1000 headlines.
Okay thanks. I'm just wondering if using a Dynamic component to just return a different header depending on the prop is a bad idea, since I actually don't need dynamism.
Should I just use the prop outside a reactive context to improve the performance? I guess I would lose reactivity, but gain performance by removing the reactivity checks.
I know I would not notice any significant difference, but I would like my code and websites to have the least performance impact as possible. I'm striving for eco-friendliness. š
i think
Dynamic
is a good idea. having non-reactive props is introducing mental overhead and possible bugs. if ur solo dev it might be fine for now, but then still I wouldn't start introducing non-reactive props, thinking about maintaining the code later down the line.
The ecological fingerprint of Dynamic
will be very tiny, especially if it doesn't upload often. I would consider it a micro-optimisation.Okay, thanks for the feedback! You are right, I also thought that I would have to put warnings for future devs that component would not react to changes to the
as
prop. It's probably too much risk for a low impact on the website's performance. Thanks for the help š
šur welcome!
You could also use
document.createElement(props.as)
and return that from your component if you don't need it to be dynamic. In Solid DOM and JSX nodes are interchangable.Thank you for the advice!