Skip to content

Commit 053bd5c

Browse files
committed
Clarify returntype
1 parent c76a3d3 commit 053bd5c

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

src/content/reference/react/captureOwnerStack.md

Lines changed: 23 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,7 +53,7 @@ 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, `null` is returned (see [Troubleshooting: The Owner Stack is `null`](#the-owner-stack-is-null)).
5757

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

@@ -299,7 +299,7 @@ pre.nowrap {
299299
<p>
300300
<pre id="error-body"></pre>
301301
</p>
302-
<h2 class="-mb-20">Owner stack:</h4>
302+
<h2 class="-mb-20">Owner Stack:</h4>
303303
<pre id="error-owner-stack" class="nowrap"></pre>
304304
<button
305305
id="error-close"
@@ -390,11 +390,7 @@ export default function App() {
390390

391391
### The Owner Stack is `null` {/*the-owner-stack-is-null*/}
392392

393-
`captureOwnerStack` was called outside of development builds. For performance reasons, React will not keep track of Owners in production.
394-
395-
### The Owner Stack is empty {/*the-owner-stack-is-empty*/}
396-
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 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.
398394

399395
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.
400396
<Sandpack>
@@ -407,7 +403,7 @@ export default function App() {
407403
// Should call `captureOwnerStack` here.
408404
function handleEvent() {
409405
// Calling it in a custom DOM event handler is too late.
410-
// The Owner Stack will be empty at this point.
406+
// The Owner Stack will be `null` at this point.
411407
console.log('Owner Stack: ', captureOwnerStack());
412408
}
413409

@@ -438,4 +434,20 @@ export default function App() {
438434
}
439435
```
440436

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

0 commit comments

Comments
 (0)