| title | children | ||||
|---|---|---|---|---|---|
| use_cases | resolving component children, normalizing JSX children, slot-like helpers, interacting with child content in library components | ||||
| tags |
|
||||
| version | 1.0 | ||||
| description | Resolve a component's `children` prop into a stable accessor. |
children resolves a component's children prop and returns a stable accessor for the resolved result.
import { children } from "solid-js";type ResolvedJSXElement = Exclude<JSX.Element, JSX.ArrayElement>;
type ResolvedChildren = ResolvedJSXElement | ResolvedJSXElement[];
function children(fn: Accessor<JSX.Element>): Accessor<ResolvedChildren> & {
toArray: () => ResolvedJSXElement[];
};- Type:
() => JSX.Element - Required: Yes
Accessor that returns the component's children value.
- Type:
Accessor<ResolvedChildren> & { toArray: () => ResolvedJSXElement[] }
Returns an accessor for the resolved children.
The accessor also exposes toArray().
- The returned accessor memoizes the resolved children, so repeated reads use the resolved result instead of recreating the child structure.
childrenresolves nested arrays, fragments, and zero-argument child accessors fromprops.children.toArray()returns the resolved children as an array. It returns[]when the resolved value isnullorundefined.
function Wrapper(props) {
const resolved = children(() => props.children);
return <div>{resolved()}</div>;
}
function Example() {
return (
<Wrapper>
<span>First</span>
<span>Second</span>
</Wrapper>
);
}function List(props) {
const resolved = children(() => props.children);
return (
<ul>
{resolved.toArray().map((child) => (
<li>{child}</li>
))}
</ul>
);
}
function Example() {
return (
<List>
<span>Alpha</span>
<span>Beta</span>
</List>
);
}