Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reactStrictMode as an option to render #1390

Merged
merged 2 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/__tests__/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,38 @@ describe('render API', () => {
`\`legacyRoot: true\` is not supported in this version of React. If your app runs React 19 or later, you should remove this flag. If your app runs React 18 or earlier, visit https://react.dev/blog/2022/03/08/react-18-upgrade-guide for upgrade instructions.`,
)
})

test('reactStrictMode in renderOptions has precedence over config when rendering', () => {
const wrapperComponentMountEffect = jest.fn()
function WrapperComponent({children}) {
React.useEffect(() => {
wrapperComponentMountEffect()
})

return children
}
const ui = <div />
configure({reactStrictMode: false})

render(ui, {wrapper: WrapperComponent, reactStrictMode: true})

expect(wrapperComponentMountEffect).toHaveBeenCalledTimes(2)
})

test('reactStrictMode in config is used when renderOptions does not specify reactStrictMode', () => {
const wrapperComponentMountEffect = jest.fn()
function WrapperComponent({children}) {
React.useEffect(() => {
wrapperComponentMountEffect()
})

return children
}
const ui = <div />
configure({reactStrictMode: true})

render(ui, {wrapper: WrapperComponent})

expect(wrapperComponentMountEffect).toHaveBeenCalledTimes(2)
})
})
32 changes: 30 additions & 2 deletions src/__tests__/renderHook.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import {renderHook} from '../pure'
import React, {useEffect} from 'react'
import {configure, renderHook} from '../pure'

const isReact18 = React.version.startsWith('18.')
const isReact19 = React.version.startsWith('19.')
Expand Down Expand Up @@ -111,3 +111,31 @@ testGateReact19('legacyRoot throws', () => {
`\`legacyRoot: true\` is not supported in this version of React. If your app runs React 19 or later, you should remove this flag. If your app runs React 18 or earlier, visit https://react.dev/blog/2022/03/08/react-18-upgrade-guide for upgrade instructions.`,
)
})

describe('reactStrictMode', () => {
let originalConfig
beforeEach(() => {
// Grab the existing configuration so we can restore
// it at the end of the test
configure(existingConfig => {
originalConfig = existingConfig
// Don't change the existing config
return {}
})
})

afterEach(() => {
configure(originalConfig)
})

test('reactStrictMode in renderOptions has precedence over config when rendering', () => {
const hookMountEffect = jest.fn()
configure({reactStrictMode: false})

renderHook(() => useEffect(() => hookMountEffect()), {
reactStrictMode: true,
})

expect(hookMountEffect).toHaveBeenCalledTimes(2)
})
})
42 changes: 35 additions & 7 deletions src/pure.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ const mountedContainers = new Set()
*/
const mountedRootEntries = []

function strictModeIfNeeded(innerElement) {
return getConfig().reactStrictMode
function strictModeIfNeeded(innerElement, reactStrictMode) {
return reactStrictMode ?? getConfig().reactStrictMode
? React.createElement(React.StrictMode, null, innerElement)
: innerElement
}
Expand All @@ -91,14 +91,24 @@ function wrapUiIfNeeded(innerElement, wrapperComponent) {

function createConcurrentRoot(
container,
{hydrate, onCaughtError, onRecoverableError, ui, wrapper: WrapperComponent},
{
hydrate,
onCaughtError,
onRecoverableError,
ui,
wrapper: WrapperComponent,
reactStrictMode,
},
) {
let root
if (hydrate) {
act(() => {
root = ReactDOMClient.hydrateRoot(
container,
strictModeIfNeeded(wrapUiIfNeeded(ui, WrapperComponent)),
strictModeIfNeeded(
wrapUiIfNeeded(ui, WrapperComponent),
reactStrictMode,
),
{onCaughtError, onRecoverableError},
)
})
Expand Down Expand Up @@ -144,17 +154,31 @@ function createLegacyRoot(container) {

function renderRoot(
ui,
{baseElement, container, hydrate, queries, root, wrapper: WrapperComponent},
{
baseElement,
container,
hydrate,
queries,
root,
wrapper: WrapperComponent,
reactStrictMode,
},
) {
act(() => {
if (hydrate) {
root.hydrate(
strictModeIfNeeded(wrapUiIfNeeded(ui, WrapperComponent)),
strictModeIfNeeded(
wrapUiIfNeeded(ui, WrapperComponent),
reactStrictMode,
),
container,
)
} else {
root.render(
strictModeIfNeeded(wrapUiIfNeeded(ui, WrapperComponent)),
strictModeIfNeeded(
wrapUiIfNeeded(ui, WrapperComponent),
reactStrictMode,
),
container,
)
}
Expand All @@ -180,6 +204,7 @@ function renderRoot(
baseElement,
root,
wrapper: WrapperComponent,
reactStrictMode,
})
// Intentionally do not return anything to avoid unnecessarily complicating the API.
// folks can use all the same utilities we return in the first place that are bound to the container
Expand Down Expand Up @@ -212,6 +237,7 @@ function render(
queries,
hydrate = false,
wrapper,
reactStrictMode,
} = {},
) {
if (onUncaughtError !== undefined) {
Expand Down Expand Up @@ -248,6 +274,7 @@ function render(
onRecoverableError,
ui,
wrapper,
reactStrictMode,
})

mountedRootEntries.push({container, root})
Expand All @@ -273,6 +300,7 @@ function render(
hydrate,
wrapper,
root,
reactStrictMode,
})
}

Expand Down
5 changes: 5 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ export interface RenderOptions<
* @see https://testing-library.com/docs/react-testing-library/api/#wrapper
*/
wrapper?: React.JSXElementConstructor<{children: React.ReactNode}> | undefined
/**
* When enabled, <StrictMode> is rendered around the inner element.
* If defined, overrides the value of `reactStrictMode` set in `configure`.
*/
reactStrictMode?: boolean
}

type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
Expand Down