Skip to content

Commit 97d018a

Browse files
committed
Show the skipped pattern and its fix in the try/catch docs
Adds a failing example with optional chaining inside try/catch, pairs it with the hoisted version, and notes that custom hooks are skipped too.
1 parent 52924f4 commit 97d018a

1 file changed

Lines changed: 23 additions & 8 deletions

File tree

‎src/content/learn/react-compiler/debugging.md‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,47 @@ Common scenarios where this occurs:
3939

4040
## Unsupported Syntax {/*unsupported-syntax*/}
4141

42-
A few syntax patterns are not supported inside `try/catch` statements yet. When React Compiler encounters one of them, it skips the whole component instead of compiling it:
42+
A few syntax patterns are not supported inside `try/catch` statements yet. When React Compiler encounters one of them, it skips the whole component or hook instead of compiling it:
4343

4444
- **Conditional expressions** - ternaries (`condition ? a : b`) and the logical operators `&&`, `||`, and `??`
4545
- **Optional chaining** - `object?.property`
4646
- **`throw` statements**
4747
- **Loops** - `for`, `for...of`, and `while` loops
4848

49-
These patterns are fully supported outside of `try/catch`, so moving them out of the `try/catch` (or extracting the fallible code into a separate function) restores compilation:
49+
These patterns are fully supported outside of `try/catch`. Even a single guard inside the `try` is enough to lose the optimization:
5050

5151
```js
5252
function WeatherReport({report}) {
53-
// ✅ Good: computed outside of try/catch
54-
const description = report.temperature > 30 ? "Warm" : "Mild";
53+
// ❌ Skipped: the guard is inside try/catch
54+
let summary;
55+
try {
56+
summary = parseSummary(report?.data ?? "");
57+
} catch (error) {
58+
summary = "Report unavailable";
59+
}
60+
61+
return <div>{summary}</div>;
62+
}
63+
```
64+
65+
Evaluate the guard before the `try`, or extract the fallible code into a separate function, and the same component compiles:
5566
56-
let summary = null;
67+
```js
68+
function WeatherReport({report}) {
69+
// ✅ Compiles: the guard runs before the try
70+
const source = report?.data ?? "";
71+
let summary;
5772
try {
58-
summary = parseSummary(report.data);
73+
summary = parseSummary(source);
5974
} catch (error) {
6075
summary = "Report unavailable";
6176
}
6277

63-
return <div>{description}: {summary}</div>;
78+
return <div>{summary}</div>;
6479
}
6580
```
6681
67-
Skipped components still build and run normally, so the only difference is that they are not optimized. To notice them, check that the component shows the ✨ badge in React DevTools, or log skipped components with the [`logger`](/reference/react-compiler/logger) option.
82+
Skipped components and hooks still build and run normally, so the only difference is that they are not optimized. To notice them, check that the component shows the ✨ badge in React DevTools, or log skipped components and hooks with the [`logger`](/reference/react-compiler/logger) option.
6883
6984
## Debugging Workflow {/*debugging-workflow*/}
7085

0 commit comments

Comments
 (0)