MCPcopy
hub / github.com/testing-library/react-testing-library / renderHook

Function renderHook

src/pure.js:320–357  ·  view source on GitHub ↗
(renderCallback, options = {})

Source from the content-addressed store, hash-verified

318}
319
320function renderHook(renderCallback, options = {}) {
321 const {initialProps, ...renderOptions} = options
322
323 if (renderOptions.legacyRoot && typeof ReactDOM.render !== 'function') {
324 const error = new Error(
325 '`legacyRoot: true` is not supported in this version of React. ' +
326 'If your app runs React 19 or later, you should remove this flag. ' +
327 'If your app runs React 18 or earlier, visit https://react.dev/blog/2022/03/08/react-18-upgrade-guide for upgrade instructions.',
328 )
329 Error.captureStackTrace(error, renderHook)
330 throw error
331 }
332
333 const result = React.createRef()
334
335 function TestComponent({renderCallbackProps}) {
336 const pendingResult = renderCallback(renderCallbackProps)
337
338 React.useEffect(() => {
339 result.current = pendingResult
340 })
341
342 return null
343 }
344
345 const {rerender: baseRerender, unmount} = render(
346 <TestComponent renderCallbackProps={initialProps} />,
347 renderOptions,
348 )
349
350 function rerender(rerenderCallbackProps) {
351 return baseRerender(
352 <TestComponent renderCallbackProps={rerenderCallbackProps} />,
353 )
354 }
355
356 return {result, rerender, unmount}
357}
358
359// just re-export everything from dom-testing-library
360export * from '@testing-library/dom'

Callers 6

renderHook.jsFile · 0.90
WrapperFunction · 0.90
testRenderHookFunction · 0.85
testRenderHookPropsFunction · 0.85
testContainerFunction · 0.85
error-handlers.jsFile · 0.85

Calls 1

renderFunction · 0.85

Tested by 1

WrapperFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…