Skip to content

Commit

Permalink
Fix runtime error when a fragment contains exactly one child.
Browse files Browse the repository at this point in the history
  • Loading branch information
jameswilddev committed Nov 25, 2021
1 parent 3f51e85 commit 16b9ad0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 9 additions & 3 deletions utilities/flattenRenderedToArray/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ export const flattenRenderedToArray = (
`type` in child &&
child.type === React.Fragment
) {
return child.props.children
? child.props.children.flatMap(recurseChildren)
: [];
if (child.props.children) {
if (Array.isArray(child.props.children)) {
return child.props.children.flatMap(recurseChildren);
} else {
return recurseChildren(child.props.children);
}
} else {
return [];
}
} else {
return [child];
}
Expand Down
4 changes: 4 additions & 0 deletions utilities/flattenRenderedToArray/unit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ test(`flattens elements`, () => {
<Text key="z">Example Content O</Text>,
]}
</div>
<React.Fragment>
<Text>Example Content P</Text>
</React.Fragment>
</React.Fragment>
);

Expand Down Expand Up @@ -75,5 +78,6 @@ test(`flattens elements`, () => {
<Text key="z">Example Content O</Text>,
]}
</div>,
<Text key="12">Example Content P</Text>,
]);
});

0 comments on commit 16b9ad0

Please sign in to comment.