Skip to content

Commit cd9a906

Browse files
committed
Troubleshooting empty stacks sandbox
1 parent 66562e8 commit cd9a906

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

src/content/reference/react/captureOwnerStack.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,45 @@ const root = createRoot(document.getElementById('root'), {
295295

296296
### The Owner Stack is empty {/*the-owner-stack-is-empty*/}
297297

298-
The call of `captureOwnerStack` happened outside of a React controlled function e.g. in a `setTimeout` callback. During render, Effects, Events, and React error handlers (e.g. `hydrateRoot#options.onCaughtError`) Owner Stacks should be available.
298+
The call of `captureOwnerStack` happened outside of a React controlled function e.g. in a `setTimeout` callback, after a fetch or in a custom DOM event handler. During render, Effects, Events, and React error handlers (e.g. `hydrateRoot#options.onCaughtError`) Owner Stacks should be available.
299+
300+
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.
301+
<Sandpack>
302+
303+
```js
304+
import {captureOwnerStack, useEffect} from 'react';
305+
306+
export default function App() {
307+
useEffect(() => {
308+
function handleEvent() {
309+
console.log('Owner Stack: ', captureOwnerStack());
310+
}
311+
312+
document.addEventListener('click', handleEvent);
313+
314+
return () => {
315+
document.removeEventListener('click', handleEvent);
316+
}
317+
})
318+
319+
return <button>Click me to see that Owner Stacks are not available in custom DOM event handlers</button>;
320+
}
321+
```
322+
323+
```json package.json hidden
324+
{
325+
"dependencies": {
326+
"react": "experimental",
327+
"react-dom": "experimental",
328+
"react-scripts": "latest"
329+
},
330+
"scripts": {
331+
"start": "react-scripts start",
332+
"build": "react-scripts build",
333+
"test": "react-scripts test --env=jsdom",
334+
"eject": "react-scripts eject"
335+
}
336+
}
337+
```
338+
339+
</Sandpack>

0 commit comments

Comments
 (0)