--- title: captureOwnerStack --- `captureOwnerStack` reads the current Owner Stack in development and returns it as a string if available. ```js const stack = captureOwnerStack(); ``` --- ## Reference {/*reference*/} ### `captureOwnerStack()` {/*captureownerstack*/} Call `captureOwnerStack` to get the current Owner Stack. ```js {5,5} import * as React from 'react'; function Component() { if (process.env.NODE_ENV !== 'production') { const ownerStack = React.captureOwnerStack(); console.log(ownerStack); } } ``` #### Parameters {/*parameters*/} `captureOwnerStack` does not take any parameters. #### Returns {/*returns*/} `captureOwnerStack` returns `string | null`. Owner Stacks are available in - Component render - Effects (e.g. `useEffect`) - React's event handlers (e.g. `
``` ```js src/errorOverlay.js export function onConsoleError({ consoleMessage, ownerStack }) { const errorDialog = document.getElementById("error-dialog"); const errorBody = document.getElementById("error-body"); const errorOwnerStack = document.getElementById("error-owner-stack"); // Display console.error() message errorBody.innerText = consoleMessage; // Display owner stack errorOwnerStack.innerText = ownerStack; // Show the dialog errorDialog.classList.remove("hidden"); } ``` ```js src/index.js active import { captureOwnerStack } from "react"; import { createRoot } from "react-dom/client"; import App from './App'; import { onConsoleError } from "./errorOverlay"; import './styles.css'; const originalConsoleError = console.error; console.error = function patchedConsoleError(...args) { originalConsoleError.apply(console, args); const ownerStack = captureOwnerStack(); onConsoleError({ // Keep in mind that in a real application, console.error can be // called with multiple arguments which you should account for. consoleMessage: args[0], ownerStack, }); }; const container = document.getElementById("root"); createRoot(container).render(); ``` ```js src/App.js function Component() { return ; } export default function App() { return ; } ``` ## Troubleshooting {/*troubleshooting*/} ### The Owner Stack is `null` {/*the-owner-stack-is-null*/} The call of `captureOwnerStack` happened outside of a React controlled function e.g. in a `setTimeout` callback, after a `fetch` call or in a custom DOM event handler. During render, Effects, React event handlers, and React error handlers (e.g. `hydrateRoot#options.onCaughtError`) Owner Stacks should be available. In the example below, clicking the button will log an empty Owner Stack because `captureOwnerStack` was called during a custom DOM event handler. The Owner Stack must be captured earlier e.g. by moving the call of `captureOwnerStack` into the Effect body. ```js import {captureOwnerStack, useEffect} from 'react'; export default function App() { useEffect(() => { // Should call `captureOwnerStack` here. function handleEvent() { // Calling it in a custom DOM event handler is too late. // The Owner Stack will be `null` at this point. console.log('Owner Stack: ', captureOwnerStack()); } document.addEventListener('click', handleEvent); return () => { document.removeEventListener('click', handleEvent); } }) return ; } ``` ### `captureOwnerStack` is not available {/*captureownerstack-is-not-available*/} `captureOwnerStack` is only exported in development builds. It will be `undefined` in production builds. If `captureOwnerStack` is used in files that are bundled for production and development, you should conditionally access it from a namespace import. ```js // Don't use named imports of `captureOwnerStack` in files that are bundled for development and production. import {captureOwnerStack} from 'react'; // Use a namespace import instead and access `captureOwnerStack` conditionally. import * as React from 'react'; if (process.env.NODE_ENV !== 'production') { const ownerStack = React.captureOwnerStack(); console.log('Owner Stack', ownerStack); } ``` --- ## Sitemap [Overview of all docs pages](/llms.txt)