Skip to content

Commit

Permalink
Remove console patching section
Browse files Browse the repository at this point in the history
It's just a (bad) polyfill for what RDT is doing. Better to lean on RDT for owner stacks in console calls.
  • Loading branch information
eps1lon committed Jan 23, 2025
1 parent bc61346 commit 1bbf3b8
Showing 1 changed file with 0 additions and 99 deletions.
99 changes: 0 additions & 99 deletions src/content/reference/react/captureOwnerStack.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,105 +188,6 @@ Check out the following example to see how to use `captureOwnerStack` to improve
- [createRoot usage: Displaying Error Boundary errors](/reference/react-dom/client/createRoot#show-a-dialog-for-uncaught-errors)
- [createRoot usage: Displaying a dialog for recoverable errors](/reference/react-dom/client/createRoot#displaying-a-dialog-for-recoverable-errors)

### Expanding error stacks {/*expanding-error-stacks*/}

In addition to the <CodeStep step={1}>stack trace of the error</CodeStep> itself, you can use <CodeStep step={2}>`captureOwnerStack`</CodeStep> to append the Owner Stack.
This can help trace the error especially when the error is caused by props. The Owner Stack helps trace the flow of props.


```js src/index.js [[1, 8, "error.stack"], [2, 7, "captureOwnerStack()"]]
import {captureOwnerStack} from 'react';
import {createRoot} from 'react-dom/client';

const root = createRoot(document.getElementById('root'), {
onUncaughtError: (error, errorInfo) => {
if (process.env.NODE_ENV !== 'production') {
const ownerStack = captureOwnerStack();
error.stack += ownerStack;
}

console.error('Uncaught', error);
},
}).render(<App />);
```

<Sandpack>

```js
function useCustomHook() {
throw new Error('Boom!');
}

function Component() {
useCustomHook();
}

export default function App() {
return <Component />;
}
```

```js src/index.js
import {captureOwnerStack} from 'react';
import {createRoot} from 'react-dom/client';
import App from './App.js';
import './styles.css';

const root = createRoot(document.getElementById('root'), {
onUncaughtError: (error, errorInfo) => {
if (process.env.NODE_ENV !== 'production') {
const ownerStack = captureOwnerStack();
error.stack =
// The stack is only split because these sandboxes don't implement
// ignore-listing 3rd party frames via sourcemaps.
// A framework would ignore-list stackframes from React via sourcemaps
// and then you could just `error.stack += ownerStack`.
// To learn more about ignore-listing see https://developer.chrome.com/docs/devtools/x-google-ignore-list
error.stack.split('\n at react-stack-bottom-frame')[0] + ownerStack;
}

// The stacks are logged instead of showing them in the UI directly to
// highlight that browsers will apply sourcemaps to the logged stacks.
// Note that sourcemapping is only applied in the real browser console not
// in the fake one displayed on this page.
console.error('Uncaught', error);
},
}).render(<App />);
```

```json package.json hidden
{
"dependencies": {
"react": "experimental",
"react-dom": "experimental",
"react-scripts": "latest"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
```

```html public/index.html hidden
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<p>Check the console output.</p>
<div id="root"></div>
</body>
</html>
```

</Sandpack>

## Troubleshooting {/*troubleshooting*/}

### The Owner Stack is `null` {/*the-owner-stack-is-null*/}
Expand Down

0 comments on commit 1bbf3b8

Please sign in to comment.