diff --git a/src/jsx/dom/index.test.tsx b/src/jsx/dom/index.test.tsx
index 2952852bc..ca3b86553 100644
--- a/src/jsx/dom/index.test.tsx
+++ b/src/jsx/dom/index.test.tsx
@@ -657,6 +657,79 @@ describe('DOM', () => {
expect(root.innerHTML).toBe('
12
')
})
+ it('empty array and non-empty array', async () => {
+ const App = () => (
+
+ {[]}
+ {[1]}
+
+ )
+ render(, root)
+ expect(root.innerHTML).toBe('1
')
+ })
+
+ it('nested array', async () => {
+ const nestedChildren: Child = [[[1], 2]]
+ const App = () => {nestedChildren}
+ render(, root)
+ expect(root.innerHTML).toBe('12
')
+ })
+
+ it('sparse array with nested child', async () => {
+ const sparseChildren: Child[] = []
+ sparseChildren[1] = [1]
+ const App = () => {sparseChildren}
+ render(, root)
+ expect(root.innerHTML).toBe('1
')
+ })
+
+ it('toggle empty array and non-empty array on update', async () => {
+ let setVisible: (value: boolean) => void = () => {}
+ const App = () => {
+ const [visible, _setVisible] = useState(false)
+ setVisible = _setVisible
+ return (
+
+ {visible ? [] : [A]}
+ {visible ? [B] : []}
+
+ )
+ }
+ render(, root)
+ expect(root.innerHTML).toBe('A
')
+
+ setVisible(true)
+ await Promise.resolve()
+ expect(root.innerHTML).toBe('B
')
+
+ setVisible(false)
+ await Promise.resolve()
+ expect(root.innerHTML).toBe('A
')
+ })
+
+ it('reshape nested array on update', async () => {
+ let setPattern: (value: number) => void = () => {}
+ const App = () => {
+ const [pattern, _setPattern] = useState(0)
+ setPattern = _setPattern
+ const children: Child =
+ pattern === 0
+ ? [[A], B]
+ : [A, [B]]
+ return {children}
+ }
+ render(, root)
+ expect(root.innerHTML).toBe('AB
')
+
+ setPattern(1)
+ await Promise.resolve()
+ expect(root.innerHTML).toBe('AB
')
+
+ setPattern(0)
+ await Promise.resolve()
+ expect(root.innerHTML).toBe('AB
')
+ })
+
it('use the same children multiple times', async () => {
const MultiChildren = ({ children }: { children: Child }) => (
<>
diff --git a/src/jsx/dom/render.ts b/src/jsx/dom/render.ts
index 145b6ac31..5d2eae62e 100644
--- a/src/jsx/dom/render.ts
+++ b/src/jsx/dom/render.ts
@@ -486,7 +486,9 @@ export const build = (context: Context, node: NodeObject, children?: Child[]): v
let prevNode: Node | undefined
for (let i = 0; i < children.length; i++) {
if (Array.isArray(children[i])) {
- children.splice(i, 1, ...(children[i] as Child[]).flat())
+ children.splice(i, 1, ...((children[i] as unknown[]).flat(Infinity) as Child[]))
+ i--
+ continue
}
let child = buildNode(children[i])
if (child) {