|
| 1 | +# Improved Error Messages for React on Rails |
| 2 | + |
| 3 | +React on Rails provides enhanced error messages with actionable solutions to help you quickly identify and fix issues. |
| 4 | + |
| 5 | +## Smart Error Messages |
| 6 | + |
| 7 | +React on Rails now provides contextual error messages that: |
| 8 | + |
| 9 | +- Identify the specific problem |
| 10 | +- Suggest concrete solutions with code examples |
| 11 | +- Offer similar component names when typos occur |
| 12 | +- Prioritize auto-bundling as the recommended approach |
| 13 | + |
| 14 | +## Auto-Bundling: The Recommended Approach |
| 15 | + |
| 16 | +React on Rails supports automatic bundling, which eliminates the need for manual component registration. |
| 17 | + |
| 18 | +### Benefits of Auto-Bundling |
| 19 | + |
| 20 | +- **No manual registration**: Components are automatically available |
| 21 | +- **Simplified development**: Just create the component file and use it |
| 22 | +- **Automatic code splitting**: Each component gets its own bundle |
| 23 | +- **Better performance**: Only load what you need |
| 24 | + |
| 25 | +### How to Use Auto-Bundling |
| 26 | + |
| 27 | +1. **Enable in your view:** |
| 28 | + |
| 29 | + ```erb |
| 30 | + <%= react_component("MyComponent", props: { data: @data }, auto_load_bundle: true) %> |
| 31 | + ``` |
| 32 | + |
| 33 | +2. **Place component in the correct directory:** |
| 34 | + |
| 35 | + ``` |
| 36 | + app/javascript/components/ |
| 37 | + └── MyComponent/ |
| 38 | + └── MyComponent.jsx # Must export default |
| 39 | + ``` |
| 40 | + |
| 41 | +3. **Generate bundles:** |
| 42 | + Bundles are automatically generated during asset precompilation via the Shakapacker precompile hook. For manual generation during development: |
| 43 | + ```bash |
| 44 | + bundle exec rake react_on_rails:generate_packs |
| 45 | + ``` |
| 46 | + |
| 47 | +That's it! No manual registration needed. |
| 48 | + |
| 49 | +## Error Message Examples |
| 50 | + |
| 51 | +### Component Not Registered |
| 52 | + |
| 53 | +**Before:** |
| 54 | + |
| 55 | +``` |
| 56 | +Component 'HelloWorld' not found |
| 57 | +``` |
| 58 | + |
| 59 | +**After:** |
| 60 | + |
| 61 | +```` |
| 62 | +❌ React on Rails Error |
| 63 | +
|
| 64 | +🔍 Problem: |
| 65 | +Component 'HelloWorld' was not found in the component registry. |
| 66 | +
|
| 67 | +💡 Suggested Solution: |
| 68 | +
|
| 69 | +🚀 Recommended: Use Auto-Bundling (No Registration Required!) |
| 70 | +
|
| 71 | +1. Enable auto-bundling in your view: |
| 72 | + <%= react_component("HelloWorld", props: {}, auto_load_bundle: true) %> |
| 73 | +
|
| 74 | +2. Place your component in the components directory: |
| 75 | + app/javascript/components/HelloWorld/HelloWorld.jsx |
| 76 | +
|
| 77 | + Component structure: |
| 78 | + components/ |
| 79 | + └── HelloWorld/ |
| 80 | + └── HelloWorld.jsx (must export default) |
| 81 | +
|
| 82 | +3. Generate the bundle: |
| 83 | + bundle exec rake react_on_rails:generate_packs |
| 84 | +
|
| 85 | +✨ That's it! No manual registration needed. |
| 86 | +
|
| 87 | +───────────────────────────────────────────── |
| 88 | +
|
| 89 | +Alternative: Manual Registration |
| 90 | +
|
| 91 | +If you prefer manual registration: |
| 92 | +1. Register in your entry file: |
| 93 | + ReactOnRails.register({ HelloWorld: HelloWorld }); |
| 94 | +
|
| 95 | +2. Import the component: |
| 96 | + import HelloWorld from './components/HelloWorld'; |
| 97 | +
|
| 98 | +3. Include the bundle in your layout (e.g., `app/views/layouts/application.html.erb`): |
| 99 | + ```erb |
| 100 | + <%= javascript_pack_tag 'application' %> |
| 101 | + <%= stylesheet_pack_tag 'application' %> |
| 102 | +```` |
| 103 | + |
| 104 | +``` |
| 105 | +
|
| 106 | +### Enhanced SSR Errors |
| 107 | +
|
| 108 | +Server-side rendering errors now include: |
| 109 | +
|
| 110 | +- Colored, formatted output for better readability |
| 111 | +- Specific error patterns detection (window/document undefined, hydration mismatches) |
| 112 | +- Actionable troubleshooting steps |
| 113 | +- Props and JavaScript code context |
| 114 | +- Console message replay |
| 115 | +
|
| 116 | +**Example SSR Error:** |
| 117 | +
|
| 118 | +``` |
| 119 | + |
| 120 | +❌ React on Rails Server Rendering Error |
| 121 | + |
| 122 | +Component: HelloWorldApp |
| 123 | + |
| 124 | +📋 Error Details: |
| 125 | +ReferenceError: window is not defined |
| 126 | + |
| 127 | +💡 Troubleshooting Suggestions: |
| 128 | + |
| 129 | +⚠️ Browser API (window/document) accessed during server render |
| 130 | + |
| 131 | +The component tried to access 'window' which doesn't exist on the server. |
| 132 | + |
| 133 | +Solutions: |
| 134 | +• Wrap browser API calls in useEffect: |
| 135 | +useEffect(() => { /_ DOM operations here _/ }, []) |
| 136 | + |
| 137 | +• Check if running in browser: |
| 138 | +if (typeof window !== 'undefined') { /_ browser code _/ } |
| 139 | + |
| 140 | +• Use dynamic import for browser-only code |
| 141 | + |
| 142 | +```` |
| 143 | +
|
| 144 | +## Ruby Configuration |
| 145 | +
|
| 146 | +### Using SmartError Directly |
| 147 | +
|
| 148 | +You can create custom smart errors in your Rails code: |
| 149 | +
|
| 150 | +```ruby |
| 151 | +raise ReactOnRails::SmartError.new( |
| 152 | + error_type: :component_not_registered, |
| 153 | + component_name: "MyComponent", |
| 154 | + additional_context: { |
| 155 | + available_components: ReactOnRails::PackerUtils.registered_components |
| 156 | + } |
| 157 | +) |
| 158 | +```` |
| 159 | + |
| 160 | +### Error Types |
| 161 | + |
| 162 | +Available error types: |
| 163 | + |
| 164 | +- `:component_not_registered` - Component not found in registry |
| 165 | +- `:missing_auto_loaded_bundle` - Auto-bundle file not found |
| 166 | +- `:hydration_mismatch` - Client/server HTML mismatch |
| 167 | +- `:server_render_error` - General SSR error |
| 168 | +- `:configuration_error` - Invalid configuration |
| 169 | + |
| 170 | +## Best Practices |
| 171 | + |
| 172 | +1. **Prefer auto-bundling** for new components to avoid registration issues |
| 173 | +2. **Use server-side rendering** to catch React component errors, hydration mismatches, and SSR-specific issues (like accessing browser APIs) during development before they reach production |
| 174 | +3. **Check error messages carefully** - they include specific solutions |
| 175 | +4. **Keep components in standard locations** for better error detection |
| 176 | + |
| 177 | +## Troubleshooting |
| 178 | + |
| 179 | +If you encounter issues: |
| 180 | + |
| 181 | +1. **Check component registration:** |
| 182 | + |
| 183 | + ```bash |
| 184 | + bundle exec rake react_on_rails:doctor |
| 185 | + ``` |
| 186 | + |
| 187 | +2. **Verify auto-bundle generation:** |
| 188 | + |
| 189 | + ```bash |
| 190 | + bundle exec rake react_on_rails:generate_packs |
| 191 | + ``` |
| 192 | + |
| 193 | +3. **Enable detailed errors** in development: |
| 194 | + ```bash |
| 195 | + FULL_TEXT_ERRORS=true rails server |
| 196 | + ``` |
| 197 | + |
| 198 | +## Related Documentation |
| 199 | + |
| 200 | +- [Auto-Bundling Guide](../core-concepts/auto-bundling-file-system-based-automated-bundle-generation.md) |
| 201 | +- [Server Rendering](../core-concepts/react-server-rendering.md) |
| 202 | +- [JavaScript API (Component Registration)](../api-reference/javascript-api.md) |
0 commit comments