Replies: 5 comments
-
@mhamri as Ryan stated in a similar issue a while back – the children are already resolved at this point, meaning those are regular DOM elements already so all you can do from there is only DOM manipulations. Please, refer to this issue for more info. I'd also like to add smth about the
It means that for your example, right after –––––– One way to achieve your goal would be to pass components as a so-called-in-a-react-world "render function" to a This is a regular way of conditional rendering of the custom components in Solid: https://docs.solidjs.com/concepts/control-flow/dynamic |
Beta Was this translation helpful? Give feedback.
-
I understand the current capabilities for handling children, but I've encountered a common scenario that feels difficult to implement elegantly in Solid. My goal is to use child components as a declarative configuration for a parent, rather than just as content to be rendered as-is. Imagine a Layout component that needs to change its structure based on the screen size. The ideal developer experience (DX) would be to write something clean and declarative like this: <Layout mode="mobile">
<Layout.Header>Sample Header</Layout.Header>
<Layout.Menu>
<Layout.Menu.Item key="1"><a href="/">Home</a></Layout.Menu.Item>
<Layout.Menu.Item key="2"><a href="/about">About</a></Layout.Menu.Item>
</Layout.Menu>
<Layout.Content>
<h1>Welcome</h1>
</Layout.Content>
<Layout.Footer>Sample Footer</Layout.Footer>
</Layout> The Layout component should be able to introspect its children and rearrange them based on the mode prop:
The core problem is that the This also means the component's structure isn't guaranteed. A user could mistakenly place the // This is possible but should be preventable by the component author
<Layout mode="mobile">
<Layout.Footer>Sample Footer</Layout.Footer>
<Layout.Header>Sample Header</Layout.Header>
{/* ... */}
</Layout> The existing solutions involve patterns like render props or passing components as props, which harm readability and create a verbose API. <Layout mode="mobile">
{(mode) => [ /* return components based on mode */ ]}
</Layout>
<Layout
mode="mobile"
header={() => <Layout.Header>Sample Header</Layout.Header>}
menu={() => (
<Layout.Menu items={[ /* menu items */ ]} />
)}
>
{/* ... */}
</Layout> Both approaches compromise the clean, declarative API that makes JSX powerful. They shift the cognitive load to the end-user, whereas the component author should be empowered to handle this complexity internally. What's needed is a mechanism for a parent to identify its children by type or key, enabling the creation of powerful and robust component APIs that follow the strategy pattern. I simplified the example to just demonstrate why parent need to have control when and how the child components get rendered, it's just an example. the point is, parent need that control that at the moment is hard to achieve. I'm asking to simplify that |
Beta Was this translation helpful? Give feedback.
-
@mhamri Oh sorry, initially I assumed it was more of a "basics" type of question. |
Beta Was this translation helpful? Give feedback.
-
Or look at Switch/Match. Match just returns the props object to the parent that does all the work. It sounds like you have complete control over children. Have them return objects that the parent will process. Props automatically get wrapped if they are reactive so you could basically defer actual rendering until you call |
Beta Was this translation helpful? Give feedback.
-
I'm going to move this to discussion because it belongs there. I don't think we have an actionable item here (at least yet). |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I expected something like this
but going up and down the internet couldn't find anything. This is very useful to be able to implement strategy pattern. Also I saw that Angular recently provided some helper functions to get info about a component or update its input.
this could help to generate different structure based on available children
Beta Was this translation helpful? Give feedback.
All reactions