Skip to content

Commit fa70867

Browse files
committed
Clarify returntype
1 parent c76a3d3 commit fa70867

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/content/reference/react/captureOwnerStack.md

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ const stack = captureOwnerStack();
2929
Call `captureOwnerStack` to get the current Owner Stack.
3030

3131
```js {5,5}
32-
import {captureOwnerStack} from 'react';
32+
import * as React from 'react';
3333

3434
function Component() {
3535
if (process.env.NODE_ENV !== 'production') {
36-
const ownerStack = captureOwnerStack();
36+
const ownerStack = React.captureOwnerStack();
3737
console.log(ownerStack);
3838
}
3939
}
@@ -53,11 +53,11 @@ Owner Stacks are available in
5353
- React's event handlers (e.g. `<button onClick={...} />`)
5454
- React error handlers ([React Root options](/reference/react-dom/client/createRoot#parameters) `onCaughtError`, `onRecoverableError`, and `onUncaughtError`)
5555

56-
If no Owner Stack is available, an empty string is returned (see [Troubleshooting: The Owner Stack is empty](#the-owner-stack-is-empty-the-owner-stack-is-empty)). Outside of development builds, `null` is returned (see [Troubleshooting: The Owner Stack is `null`](#the-owner-stack-is-null-the-owner-stack-is-null)).
56+
If no Owner Stack is available, an empty string or `null` is returned (see [Troubleshooting: The Owner Stack is empty](#the-owner-stack-is-empty-the-owner-stack-is-empty)).
5757

5858
#### Caveats {/*caveats*/}
5959

60-
- Owner Stacks are only available in development. `captureOwnerStack` will always return `null` outside of development.
60+
- Owner Stacks are only available in development. `captureOwnerStack` will not be exported in production builds (see [`captureOwnerStack` is not available](#captureownerstack-is-not-available)).
6161

6262
<DeepDive>
6363

@@ -388,13 +388,10 @@ export default function App() {
388388

389389
## Troubleshooting {/*troubleshooting*/}
390390

391-
### The Owner Stack is `null` {/*the-owner-stack-is-null*/}
392-
393-
`captureOwnerStack` was called outside of development builds. For performance reasons, React will not keep track of Owners in production.
394-
395391
### The Owner Stack is empty {/*the-owner-stack-is-empty*/}
396392

397-
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, React event handlers, and React error handlers (e.g. `hydrateRoot#options.onCaughtError`) Owner Stacks should be available.
393+
The call to `captureOwnerStack()` returned `null` or the empty string (`''`).
394+
Owner Stacks are not available 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, React event handlers, and React error handlers (e.g. `hydrateRoot#options.onCaughtError`) Owner Stacks should be available.
398395

399396
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.
400397
<Sandpack>
@@ -408,7 +405,7 @@ export default function App() {
408405
function handleEvent() {
409406
// Calling it in a custom DOM event handler is too late.
410407
// The Owner Stack will be empty at this point.
411-
console.log('Owner Stack: ', captureOwnerStack());
408+
console.log('Owner Stack: ', String(captureOwnerStack()));
412409
}
413410

414411
document.addEventListener('click', handleEvent);
@@ -438,4 +435,20 @@ export default function App() {
438435
}
439436
```
440437

441-
</Sandpack>
438+
</Sandpack>
439+
440+
### `captureOwnerStack` is not available {/*captureownerstack-is-not-available*/}
441+
442+
`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.
443+
444+
```js
445+
// Don't use named imports of `captureOwnerStack` in files that are bundled for development and production.
446+
import {captureOwnerStack} from 'react';
447+
// Use a namespace import instead and access `captureOwnerStack` conditionally.
448+
import * as React from 'react';
449+
450+
if ('captureOwnerStack' in React) {
451+
const ownerStack = React.captureOwnerStack();
452+
console.log(ownerStack);
453+
}
454+
```

0 commit comments

Comments
 (0)