Determine component type
With react it's possible to tag a component with a type:
In the card component:
Now I can find that Card.Section, again in card component:
Unfortunately, it doesn't seem possible with solidjs.
What I have done to determine whether a component is a Card.Section. Within CardSection.tsx
I'm setting:
'card-section': true
Then to find it:
But is there a more idiomatic way?9 Replies
It depends on what your goal is, why do need to find reference to Card.Section? Usually the preferred way of dealing with sub components is to use createContext. Then you can avoid things like looping over them and resoving them with children().
An example is:
Some content can be Card.Sections or divs or other components entirely.
I'm using children() already 😄
this is what I have so far:
This isn't a very idiomatic pattern in solid
Since there's no vdom, there's not really support for this exactly to mimic React
That's what GPT has told me. But I was hoping someone with more experience would know of a solution.
Just as a refresher for myself, what's the problem that this solves? I haven't used React in a bit. Is it just to add additional props to the children?
The intent is for the card component to loop the children. Any child that is marked as CardSection has then additional props added to it. Then all children collected and are children to another component in the tree.
What I have works fine. I just wanted to double check if there was a better way.
You could add a "registerSection" function to the context that increments an index and the sections just use the index to set the data attributes themselves
Probably also need a
totalSections
signal actually in the card
Thanks, but I'm going to stick with my code.
having
having
'card-section': true
in the CardSection means it's totally transparent and the rest of the code stays the sameJust bear in mind that if someone uses Show or For inside the Card component it will trigger a complete recreation of CardSelections and that may lead to some undesired behavior like focus loosing and such.