Skip to content

Commit 080aed3

Browse files
committed
React: fix layout shortcut
1 parent f7787d5 commit 080aed3

File tree

5 files changed

+19
-28
lines changed

5 files changed

+19
-28
lines changed

packages/react/src/App.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import { createElement, FunctionComponent, ReactNode, useEffect, useMemo, useState } from 'react'
1111
import HeadContext from './HeadContext'
1212
import PageContext from './PageContext'
13-
import { LayoutFunction, ReactComponent, ReactPageHandlerArgs } from './types'
13+
import { LayoutComponent, LayoutFunction, ReactComponent, ReactPageHandlerArgs } from './types'
1414

1515
let currentIsInitialPage = true
1616
let routerIsInitialized = false
@@ -101,19 +101,22 @@ export default function App<SharedProps extends PageProps = PageProps>({
101101
children ||
102102
(({ Component, props, key }) => {
103103
const child = createElement(Component, { key, ...props })
104-
105-
if (typeof Component.layout === 'function') {
106-
return (Component.layout as LayoutFunction)(child)
107-
}
108-
109-
if (Array.isArray(Component.layout)) {
110-
return (Component.layout as any)
111-
.concat(child)
112-
.reverse()
113-
.reduce((children: any, Layout: any) => createElement(Layout, { children, ...props }))
104+
const layout = Component.layout
105+
if (!layout) return child
106+
107+
if (typeof layout === 'function') {
108+
try {
109+
return (layout as LayoutFunction)(child)
110+
} catch {
111+
return createElement(layout as LayoutComponent, { ...props, children: child })
112+
}
114113
}
115114

116-
return child
115+
const layouts = Array.isArray(layout) ? layout : [layout]
116+
return layouts.reduceRight<ReactNode>(
117+
(acc, Layout) => createElement(Layout as LayoutComponent, { ...props, children: acc }),
118+
child,
119+
)
117120
})
118121

119122
return createElement(

packages/react/test-app/Pages/PersistentLayouts/Shorthand/Nested/PageA.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ const PageA = () => {
1313
)
1414
}
1515

16-
PageA.layout = (page: React.ReactNode) => {
17-
return (
18-
<SiteLayout>
19-
<NestedLayout children={page} />
20-
</SiteLayout>
21-
)
22-
}
16+
PageA.layout = [SiteLayout, NestedLayout]
2317

2418
export default PageA

packages/react/test-app/Pages/PersistentLayouts/Shorthand/Nested/PageB.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ const PageB = () => {
1313
)
1414
}
1515

16-
PageB.layout = (page: React.ReactNode) => {
17-
return (
18-
<SiteLayout>
19-
<NestedLayout children={page} />
20-
</SiteLayout>
21-
)
22-
}
16+
PageB.layout = [SiteLayout, NestedLayout]
2317

2418
export default PageB

packages/react/test-app/Pages/PersistentLayouts/Shorthand/Simple/PageA.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ const PageA = () => {
1212
)
1313
}
1414

15-
PageA.layout = (page: React.ReactNode) => <SiteLayout children={page} />
15+
PageA.layout = SiteLayout
1616

1717
export default PageA

packages/react/test-app/Pages/PersistentLayouts/Shorthand/Simple/PageB.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ const PageB = () => {
1212
)
1313
}
1414

15-
PageB.layout = (page: React.ReactNode) => <SiteLayout children={page} />
15+
PageB.layout = SiteLayout
1616

1717
export default PageB

0 commit comments

Comments
 (0)