|
| 1 | +# React 19 Quick Reference |
| 2 | + |
| 3 | +> For the complete guide, see [React 19 Upgrade Guide](./react-19-upgrade-guide.md) |
| 4 | +
|
| 5 | +## Pre-Flight Checklist |
| 6 | + |
| 7 | +```bash |
| 8 | +# ✅ Check current versions |
| 9 | +node --version # Should be 18+ |
| 10 | +ruby --version # Should be 3.2+ |
| 11 | + |
| 12 | +# ✅ Check dependencies |
| 13 | +bundle info react_on_rails # Should be 16.1.1+ |
| 14 | +bundle info shakapacker # Should be 9.0+ |
| 15 | +``` |
| 16 | + |
| 17 | +## Upgrade Commands |
| 18 | + |
| 19 | +```bash |
| 20 | +# 1. Update React |
| 21 | + |
| 22 | +yarn add -D @types/react@19 @types/react-dom@19 |
| 23 | + |
| 24 | +# 2. Update React on Rails (if needed) |
| 25 | +bundle update react_on_rails |
| 26 | + |
| 27 | +# 3. Rebuild |
| 28 | +rm -rf public/packs node_modules/.cache |
| 29 | +yarn install |
| 30 | +bin/rails assets:precompile |
| 31 | +``` |
| 32 | + |
| 33 | +## Common Import Fixes |
| 34 | + |
| 35 | +### ❌ BROKEN (with esModuleInterop: false) |
| 36 | + |
| 37 | +```typescript |
| 38 | +import * as React from 'react'; |
| 39 | + |
| 40 | +class MyComponent extends React.Component {} |
| 41 | +const [count, setCount] = React.useState(0); |
| 42 | +const context = React.createContext(null); |
| 43 | +``` |
| 44 | + |
| 45 | +**Error**: `export 'createContext' was not found in 'react'` |
| 46 | + |
| 47 | +### ✅ FIXED - Option A: Enable esModuleInterop |
| 48 | + |
| 49 | +```json |
| 50 | +{ |
| 51 | + "compilerOptions": { |
| 52 | + "esModuleInterop": true, |
| 53 | + "allowSyntheticDefaultImports": true |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +```typescript |
| 59 | +import React, { useState, createContext } from 'react'; |
| 60 | + |
| 61 | +class MyComponent extends React.Component {} |
| 62 | +const [count, setCount] = useState(0); |
| 63 | +const context = createContext(null); |
| 64 | +``` |
| 65 | + |
| 66 | +### ✅ FIXED - Option B: Use Named Imports |
| 67 | + |
| 68 | +```typescript |
| 69 | +import { Component, useState, createContext } from 'react'; |
| 70 | + |
| 71 | +class MyComponent extends Component {} |
| 72 | +const [count, setCount] = useState(0); |
| 73 | +const context = createContext(null); |
| 74 | +``` |
| 75 | + |
| 76 | +## RSC Components (Pro) |
| 77 | + |
| 78 | +```typescript |
| 79 | +// ✅ Server Component - NO 'use client' |
| 80 | +export default async function ServerComponent() { |
| 81 | + const data = await fetchData(); |
| 82 | + return <div>{data}</div>; |
| 83 | +} |
| 84 | + |
| 85 | +// ✅ Client Component - HAS 'use client' |
| 86 | +'use client'; |
| 87 | + |
| 88 | +import { useState } from 'react'; |
| 89 | + |
| 90 | +export default function ClientComponent() { |
| 91 | + const [count, setCount] = useState(0); |
| 92 | + return <button onClick={() => setCount(count + 1)}>{count}</button>; |
| 93 | +} |
| 94 | + |
| 95 | +// ✅ RSC Provider/Route - MUST use named imports |
| 96 | +'use client'; |
| 97 | + |
| 98 | +import { createContext, useContext } from 'react'; |
| 99 | + |
| 100 | +const MyContext = createContext(null); |
| 101 | +export const useMyContext = () => useContext(MyContext); |
| 102 | +``` |
| 103 | + |
| 104 | +## Build Verification |
| 105 | + |
| 106 | +```bash |
| 107 | +# Should complete without React import errors |
| 108 | +bin/shakapacker |
| 109 | + |
| 110 | +# Check for these errors (should be NONE): |
| 111 | +# ❌ export 'createContext' was not found |
| 112 | +# ❌ export 'useContext' was not found |
| 113 | +# ❌ export 'Component' was not found |
| 114 | +``` |
| 115 | + |
| 116 | +## Troubleshooting |
| 117 | + |
| 118 | +| Error | Quick Fix | |
| 119 | +| ------------------------------------------ | ------------------------------------------------------ | |
| 120 | +| `export 'X' was not found in 'react'` | Use named imports or enable `esModuleInterop` | |
| 121 | +| `Objects are not valid as a React child` | Add second param to render function | |
| 122 | +| `Functions are not valid as a React child` | Return React element, not function | |
| 123 | +| Build fails in production | Clear cache: `rm -rf node_modules/.cache public/packs` | |
| 124 | + |
| 125 | +## TypeScript Config |
| 126 | + |
| 127 | +### Recommended (Easy Mode) |
| 128 | + |
| 129 | +```json |
| 130 | +{ |
| 131 | + "compilerOptions": { |
| 132 | + "target": "ES2020", |
| 133 | + "lib": ["ES2020", "DOM"], |
| 134 | + "jsx": "react-jsx", |
| 135 | + "esModuleInterop": true, |
| 136 | + "allowSyntheticDefaultImports": true, |
| 137 | + "moduleResolution": "bundler" |
| 138 | + } |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +### Advanced (esModuleInterop: false) |
| 143 | + |
| 144 | +```json |
| 145 | +{ |
| 146 | + "compilerOptions": { |
| 147 | + "target": "ES2020", |
| 148 | + "lib": ["ES2020", "DOM"], |
| 149 | + "jsx": "react-jsx", |
| 150 | + "esModuleInterop": false, |
| 151 | + "moduleResolution": "bundler" |
| 152 | + } |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +**MUST use named imports everywhere!** |
| 157 | + |
| 158 | +## Rollback |
| 159 | + |
| 160 | +```bash |
| 161 | + |
| 162 | +yarn add -D @types/react@18 @types/react-dom@18 |
| 163 | +rm -rf public/packs |
| 164 | +bin/rails assets:precompile |
| 165 | +``` |
| 166 | + |
| 167 | +## Need Help? |
| 168 | + |
| 169 | +- [Full React 19 Upgrade Guide](./react-19-upgrade-guide.md) |
| 170 | +- [React on Rails Issues](https://github.com/shakacode/react_on_rails/issues) |
| 171 | +- [ShakaCode Forum](https://forum.shakacode.com) |
| 172 | +- [Commercial Support](https://www.shakacode.com/contact) |
0 commit comments