@@ -13,7 +13,7 @@ description: >-
1313 Resolve a component's `children` prop into a stable accessor.
1414---
1515
16- ` children ` resolves a component's ` children ` prop and returns an accessor for the resolved result.
16+ ` children ` resolves a component's ` children ` prop and returns a stable accessor for the resolved result.
1717
1818## Import
1919
@@ -24,10 +24,8 @@ import { children } from "solid-js";
2424## Type
2525
2626``` ts
27- function children(fn : Accessor <JSX .Element >): ChildrenReturn ;
28-
29- type ChildrenReturn = Accessor <ResolvedChildren > & {
30- toArray: () => ResolvedChildren [];
27+ function children(fn : Accessor <JSX .Element >): Accessor <JSX .Element > & {
28+ toArray: () => JSX .Element [];
3129};
3230```
3331
@@ -42,16 +40,16 @@ Accessor that returns the component's `children` value.
4240
4341## Return value
4442
45- - ** Type:** ` ChildrenReturn `
43+ - ** Type:** ` Accessor<JSX.Element> & { toArray: () => JSX.Element[] } `
4644
4745Returns an accessor for the resolved children.
4846The accessor also exposes ` toArray() ` .
4947
5048## Behavior
5149
52- - ` children ` resolves functions, arrays, fragments, and nested child structures .
53- - The returned accessor reads the current resolved value of ` props.children ` .
54- - ` toArray() ` returns a flattened array of resolved child elements .
50+ - The returned accessor memoizes the resolved children, so repeated reads use the resolved result instead of recreating the child structure .
51+ - ` children ` resolves functions, arrays, fragments, and nested child structures from ` props.children ` .
52+ - ` toArray() ` returns the resolved children as an array. It returns ` [] ` when the resolved value is ` null ` or ` undefined ` .
5553
5654## Examples
5755
@@ -63,7 +61,12 @@ import { children } from "solid-js";
6361function Wrapper(props ) {
6462 const resolved = children (() => props .children );
6563
66- return <div >{ resolved ()} </div >;
64+ return (
65+ <div >
66+ <header >{ resolved ()} </header >
67+ <div >{ resolved ()} </div >
68+ </div >
69+ );
6770}
6871```
6972
@@ -74,11 +77,10 @@ import { children } from "solid-js";
7477
7578function List(props ) {
7679 const resolved = children (() => props .children );
77- const items = resolved .toArray ();
7880
7981 return (
8082 <ul >
81- { items .map ((child ) => (
83+ { resolved . toArray () .map ((child ) => (
8284 <li >{ child } </li >
8385 ))}
8486 </ul >
@@ -88,4 +90,4 @@ function List(props) {
8890
8991## Related
9092
91- - [ ` createContext ` ] ( /reference/component-apis/create-context )
93+ - [ Props ] ( /concepts/components/props )
0 commit comments