You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/react-compiler.md
+74-2Lines changed: 74 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -346,9 +346,81 @@ function handleFormInputEnter(event: KeyboardEvent) {
346
346
}
347
347
```
348
348
349
-
##Verifying fixes (for LLMs)
349
+
### Optional chaining in try/catch blocks
350
350
351
-
When fixing violations programmatically, use one of these methods to verify the fix was successful:
351
+
> Reason: Support value blocks (conditional, logical, optional chaining, etc) within a try/catch statement
352
+
353
+
The compiler can't handle optional chaining (`?.()`) inside try/catch blocks.
354
+
355
+
**Before:**
356
+
357
+
```typescript
358
+
try {
359
+
awaitdoSomething()
360
+
onSuccessCallback?.()
361
+
} catch (error) {
362
+
onFailureCallback?.(error)
363
+
}
364
+
```
365
+
366
+
**After:**
367
+
368
+
```typescript
369
+
try {
370
+
awaitdoSomething()
371
+
if (onSuccessCallback) {
372
+
onSuccessCallback()
373
+
}
374
+
} catch (error) {
375
+
if (onFailureCallback) {
376
+
onFailureCallback(error)
377
+
}
378
+
}
379
+
```
380
+
381
+
### Mutable objects created with useMemo
382
+
383
+
> Reason: This value cannot be modified. Modifying a value previously passed as an argument to a hook is not allowed.
384
+
385
+
Creating mutable objects (Web Workers, stores, etc.) with `useMemo` and then mutating them in `useEffect` triggers a compiler error because the compiler tracks values through hooks.
386
+
387
+
**Before:**
388
+
389
+
```typescript
390
+
const worker =useMemo(() =>createWebWorker(), [])
391
+
392
+
useEffect(() => {
393
+
worker.onmessage=handleMessage// Error: mutating hook return value
394
+
}, [worker])
395
+
```
396
+
397
+
**After:**
398
+
399
+
```typescript
400
+
const workerRef =useRef<Worker|null>(null)
401
+
402
+
function getWorker():Worker {
403
+
if (!workerRef.current) {
404
+
workerRef.current=createWebWorker()
405
+
}
406
+
returnworkerRef.current
407
+
}
408
+
409
+
useEffect(() => {
410
+
const worker =getWorker()
411
+
worker.onmessage=handleMessage
412
+
return () => {
413
+
worker.onmessage=null
414
+
}
415
+
// getWorker is non-reactive (only accesses a stable ref), so it's safe to omit from deps
416
+
}, [])
417
+
```
418
+
419
+
## Identifying violations and verifying fixes (for LLMs)
420
+
421
+
When fixing violations programmatically, first identify modules with violations by checking `.react-compiler.rec.json`, then use Babel with Inline Logger (method 2 below) on the file to see the exact errors. Extract the error reason and location to plan the fix.
422
+
423
+
Once the fix is applied, verify it using one of the following methods:
0 commit comments