Skip to content

Commit 2679657

Browse files
authored
chore: Update React Compiler adoption guidelines (#1000)
This file is automatically synced from the `shared-configs` repository. Source: https://github.com/doist/shared-configs/blob/main/
1 parent af7ce99 commit 2679657

File tree

1 file changed

+74
-2
lines changed

1 file changed

+74
-2
lines changed

docs/react-compiler.md

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,81 @@ function handleFormInputEnter(event: KeyboardEvent) {
346346
}
347347
```
348348

349-
## Verifying fixes (for LLMs)
349+
### Optional chaining in try/catch blocks
350350

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+
await doSomething()
360+
onSuccessCallback?.()
361+
} catch (error) {
362+
onFailureCallback?.(error)
363+
}
364+
```
365+
366+
**After:**
367+
368+
```typescript
369+
try {
370+
await doSomething()
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+
return workerRef.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:
352424

353425
**1. CLI Tool (recommended)**
354426

0 commit comments

Comments
 (0)