Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add render prop support to
<Main>
(#30156)
Adds support for render props to the `<Main>` component, when using the [functional custom `Document`](#28515) style. This allows you to write something like this: ```tsx export default function Document() { const jsxStyleRegistry = createStyleRegistry() return ( <Html> <Head /> <body> <Main> {content => ( <StyledJsxWrapper registry={jsxStyleRegistry}> {content} </StyledJsxWrapper> )} </Main> <NextScript /> </body> </Html> ) } ``` In functional document components, this allows the `<App>` to be wrapped, similar to `enhanceApp` (which is only available via `getInitialProps`, which is not supported by functional document components). The primary use for this is for integrating with 3rd party CSS-in-JS libraries, allowing them to attach an `useFlush` handler to [support React 18](reactwg/react-18#110): ```tsx import { unstable_useFlush as useFlush } from 'next/document' export default function StyledJsxWrapper({ children, registry }) { useFlush(() => { /* ... */ }) return ( <StyleRegistry registry={registry}> {children} </StyleRegistry> ) } ``` Support for `useFlush` will be added in a follow up PR.
- Loading branch information
Showing
8 changed files
with
122 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
test/integration/document-functional-render-prop/lib/context.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { createContext } from 'react' | ||
|
||
export default createContext(null) |
20 changes: 20 additions & 0 deletions
20
test/integration/document-functional-render-prop/pages/_document.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Html, Head, Main, NextScript } from 'next/document' | ||
import Context from '../lib/context' | ||
|
||
export default function Document() { | ||
return ( | ||
<Html> | ||
<Head /> | ||
<body> | ||
<Main> | ||
{(children) => ( | ||
<Context.Provider value="from render prop"> | ||
{children} | ||
</Context.Provider> | ||
)} | ||
</Main> | ||
<NextScript /> | ||
</body> | ||
</Html> | ||
) | ||
} |
7 changes: 7 additions & 0 deletions
7
test/integration/document-functional-render-prop/pages/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { useContext } from 'react' | ||
import Context from '../lib/context' | ||
|
||
export default function MainRenderProp() { | ||
const value = useContext(Context) | ||
return <span>{value}</span> | ||
} |
24 changes: 24 additions & 0 deletions
24
test/integration/document-functional-render-prop/tests/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* eslint-env jest */ | ||
|
||
import { join } from 'path' | ||
import { findPort, launchApp, killApp, renderViaHTTP } from 'next-test-utils' | ||
|
||
const appDir = join(__dirname, '..') | ||
let appPort | ||
let app | ||
|
||
describe('Functional Custom Document', () => { | ||
describe('development mode', () => { | ||
beforeAll(async () => { | ||
appPort = await findPort() | ||
app = await launchApp(appDir, appPort) | ||
}) | ||
|
||
afterAll(() => killApp(app)) | ||
|
||
it('supports render props', async () => { | ||
const html = await renderViaHTTP(appPort, '/') | ||
expect(html).toMatch(/<span>from render prop<\/span>/) | ||
}) | ||
}) | ||
}) |