Passing For Id between InputGroup and Input using children

I'm trying to pass create a JSX structure like:
<InputGroup
label="Email"
error="that's not an email"
>
<Input
placeholder="like@this.com"
/>
</InputGroup>
<InputGroup
label="Email"
error="that's not an email"
>
<Input
placeholder="like@this.com"
/>
</InputGroup>
My InputGroup component looks like:
export default function InputGroup(props: InputGroupProps) {
const { label, error, children: childrenProp } = props;
const [forId] = createSignal(`input-${Math.random().toString(36).substr(2, 9)}`);
const renderedChildren = children(() => childrenProp);

createEffect(() => {
const child = renderedChildren();
if (child?.props) {
child.props.id = forId();
}
});

return (
<>
<label for={forId()}>{label}</label>
{renderedChildren()}
<Show when={!!error}>
<p>{error}</p>
</Show>
</>
);
}
export default function InputGroup(props: InputGroupProps) {
const { label, error, children: childrenProp } = props;
const [forId] = createSignal(`input-${Math.random().toString(36).substr(2, 9)}`);
const renderedChildren = children(() => childrenProp);

createEffect(() => {
const child = renderedChildren();
if (child?.props) {
child.props.id = forId();
}
});

return (
<>
<label for={forId()}>{label}</label>
{renderedChildren()}
<Show when={!!error}>
<p>{error}</p>
</Show>
</>
);
}
This is if (child?.props) { is giving me the type error:
Property 'props' does not exist on type 'number | boolean | Node | (string & {}) | ResolvedJSXElement[]'.
Property 'props' does not exist on type 'number | boolean | Node | (string & {}) | ResolvedJSXElement[]'.
Does anyone have ideas how to achieve what I'm trying to do with the children helper? Or is there a better approach?
2 Replies
Chronove
Chronove16mo ago
- Element props should not be destructured, else they will lose reactivity - You c/s-hould use the error render as a fallback (it's an attribute of the Show Element) - instead of creating your own forId you should use createUniqueId() (https://www.solidjs.com/docs/latest/api#createuniqueid) or even just a createMemo instead of a signal which never gets changed For the error you're getting, I don't know the types of child or InputGroupProps so I can only guess that the types aren't correct, as you're error "Property props does not exist..." sounds like a typescript error to me.
Otonashi
Otonashi16mo ago
there are no props on thr child it's just a dom element (if you're passing a single dom element as children) you don't need a signal or memo for the id, just assign it to a variable, and use createUniqueId which makes it identical on both the client and server to be on the safe side you should check that child is actually a dom element; iterate over it if it is an array, and ignore it if it is anything else
Want results from more Discord servers?
Add your server
More Posts
Are there any guarantees on the order of createMemo / createComputed execution when siblings?This example suggests there are none: https://playground.solidjs.com/anonymous/32dc8fda-6b7d-40df-8ccreateMemo with `equals: false` not reactive when mutating referenceI am trying to prevent to create `DOMMatrix` with each calculation, so I thought to mutate 1 with `cHow to have an effect that does not subscribe to the signal's values?I have this effect: https://github.com/nikitavoloboev/kuskus/blob/main/src/components/Page.tsx#L15 multiple sitesdoes solid start support multiple sites (on different domains) but with one node instance? is there createResource mutations / page stateHello, For I'm using createResource for my solid-start project for fetching post user can interact Why autofocus on input element not work with solid second timeI have this code: https://github.com/nikitavoloboev/kuskus Here I do conditional render of `NewTodoPass ref in routeDataHappy Saturday. Struggling with what I hope is a simple problem. I'm trying to get a ref passed tIs there an easy way to render React inside Solid?I've got a landing page generated through an external tool that I'd rather just keep in React so I dCannot read properties of null (reading 'nextSibling')Hello, I got this error in production (it was working, then no changes occured), and now I am gettinSolidStart: Get Rendered Route Instead in useLocation()Hello! I'm new to SolidJS and SolidStart. I was wondering if it is possible to get the rendered rou