Skip to content

Commit 5c09a4c

Browse files
committed
getting-started.md - justin manual done
1 parent 7b40c80 commit 5c09a4c

File tree

3 files changed

+119
-80
lines changed

3 files changed

+119
-80
lines changed

docs/getting-started.md

Lines changed: 116 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,38 @@
11
# Getting Started
22

3-
Note, the best way to understand how to use ReactOnRails is to study a few simple examples. You can do a quick demo setup, either on your existing app or on a new Rails app.
3+
> **💡 Looking for the fastest way to get started?** Try our **[15-Minute Quick Start Guide](./quick-start/README.md)** instead.
44
5-
This documentation assumes the usage of ReactOnRails with Shakapacker 7. For installation on Shakapacker 6, check [tips for usage with Shakapacker 6](./additional-details/tips-for-usage-with-sp6.md) first.
5+
## Choose Your Starting Point
66

7-
1. Do the quick [tutorial](./guides/tutorial.md).
8-
2. Add React on Rails to an existing Rails app per [the instructions](./guides/installation-into-an-existing-rails-app.md).
9-
3. Look at [spec/dummy](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy), a simple, no DB example.
10-
4. Look at [github.com/shakacode/react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial); it's a full-featured example live at [reactrails.com](https://reactrails.com).
7+
The best way to understand React on Rails depends on your situation:
8+
9+
### 🚀 **New to React on Rails?**
10+
11+
**[15-Minute Quick Start](./quick-start/README.md)** - Get your first component working fast
12+
13+
### 📱 **Have an existing Rails app?**
14+
15+
**[Add to Existing App](./guides/installation-into-an-existing-rails-app.md)** - Integrate React on Rails
16+
17+
### 📚 **Want comprehensive tutorial?**
18+
19+
**[Complete Tutorial](./guides/tutorial.md)** - Step-by-step with Redux and routing
20+
21+
### 👀 **Learn by example?**
22+
23+
- **[Spec/Dummy](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy)** - Simple example in this repo
24+
- **[Live Demo](https://reactrails.com)** with **[source code](https://github.com/shakacode/react-webpack-rails-tutorial)**
25+
26+
---
27+
28+
## System Requirements
29+
30+
**Rails 7+** (Rails 5+ supported)
31+
**Ruby 3.0+** (Ruby 2.7+ supported)
32+
**Node.js 18+**
33+
**Shakapacker 7** (or Shakapacker 6 with [special setup](./additional-details/tips-for-usage-with-sp6.md))
34+
35+
> **Don't have Shakapacker?** It's the modern replacement for Webpacker and required for React on Rails.
1136
1237
## Basic Installation
1338

@@ -16,11 +41,9 @@ You need a Rails application with Shakapacker installed and configured on it. Ch
1641
```bash
1742
rails new PROJECT_NAME --skip-javascript
1843
cd PROJECT_NAME
19-
bundle add shakapacker --strict
20-
rails shakapacker:install
2144
```
2245

23-
You may need to check [the instructions for installing into an existing Rails app](./guides/installation-into-an-existing-rails-app.md) if you have an already working Rails application.
46+
## React on Rails Installation
2447

2548
1. Add the `react_on_rails` gem to Gemfile:
2649
Please use [the latest version](https://rubygems.org/gems/react_on_rails) to ensure you get all the security patches and the best support.
@@ -31,40 +54,28 @@ You may need to check [the instructions for installing into an existing Rails ap
3154

3255
Commit this to git (or else you cannot run the generator in the next step unless you pass the option `--ignore-warnings`).
3356

34-
2. Run the install generator:
35-
3657
```bash
37-
rails generate react_on_rails:install
58+
git add -A
59+
git commit -m "Initial commit"
3860
```
3961

40-
3. Start the app:
41-
42-
- Run `./bin/dev` for HMR
43-
- Run `./bin/dev static` for statically created bundles (no HMR)
44-
45-
4. Visit http://localhost:3000/hello_world.
46-
47-
### Turning on server rendering
48-
49-
With the code from running the React on Rails generator above:
62+
2. Run the install generator:
5063

51-
1. Edit `app/views/hello_world/index.html.erb` and set the `prerender` option to `true`.
64+
``bash
65+
bundle exec rails generate react_on_rails:install
5266

53-
You may need to use `Node` as your js runtime environment by setting `EXECJS_RUNTIME=Node` into your environment variables.
67+
```
5468
55-
2. Refresh the page.
69+
```
5670

57-
Below is the line where you turn server rendering on by setting `prerender` to true:
71+
Start the app:
5872

59-
```erb
60-
<%= react_component("HelloWorld", props: @hello_world_props, prerender: true) %>
73+
```bash
74+
bin/dev help
75+
bin/dev # start with hmr
76+
bin/dev static #
6177
```
6278

63-
Note, if you got an error in your console regarding "ReferenceError: window is not defined",
64-
then you need to edit `config/shakapacker.yml` and set `hmr: false` and `inline: false`.
65-
See [rails/webpacker PR 2644](https://github.com/rails/webpacker/pull/2644) for a fix for this
66-
issue.
67-
6879
## Basic Usage
6980

7081
### Configuration
@@ -75,34 +86,62 @@ issue.
7586

7687
## Including your React Component on your Rails Views
7788

78-
- React components are rendered via your Rails Views. Here's an ERB sample:
89+
Once installation is complete, you can render a React component in any Rails view using the `react_component` helper method.
7990

80-
```erb
81-
<%= react_component("HelloWorld", props: @some_props) %>
82-
```
91+
```erb
92+
<%= react_component("HelloWorld", props: @some_props) %>
93+
```
8394

8495
- **Server-Side Rendering**: Your React component is first rendered into HTML on the server. Use the **prerender** option:
8596

8697
```erb
8798
<%= react_component("HelloWorld", props: @some_props, prerender: true) %>
8899
```
89100

90-
- The `component_name` parameter is a string matching the name you used to expose your React component globally. So, in the above examples, if you had a React component named "HelloWorld", you would register it with the following lines:
101+
`@component_name` is a string and corresponds to the name you used to globally expose your React component.
91102

92-
```js
93-
import ReactOnRails from 'react-on-rails';
94-
import HelloWorld from './HelloWorld';
95-
ReactOnRails.register({ HelloWorld });
96-
```
103+
## Auto-Bundling (includes Auto-Registration)
97104

98-
Exposing your component in this way allows you to reference the component from a Rails view. You can expose as many components as you like, but their names must be unique. See below for the details of how you expose your components via the React on Rails Webpack configuration. You may call `ReactOnRails.register` many times.
105+
React on Rails supports **Auto-Bundling**, which automatically creates the webpack bundle _and_ registers your React components. This means you don’t have to manually configure packs or call `ReactOnRails.register(...)`.
99106

100-
- `@some_props` can be either a hash or JSON string. This is an optional argument assuming you do not need to pass any options (if you want to pass options, such as `prerender: true`, but you do not want to pass any properties, simply pass an empty hash `{}`). This will make the data available in your component:
107+
---
101108

102-
```erb
103-
# Rails View
104-
<%= react_component("HelloWorld", props: { name: "Stranger" }) %>
105-
```
109+
### Manual Registration (traditional way)
110+
111+
```js
112+
// packs/hello-world-bundle.js
113+
import ReactOnRails from 'react-on-rails';
114+
import HelloWorld from '../components/HelloWorld';
115+
116+
ReactOnRails.register({ HelloWorld });
117+
```
118+
119+
```erb
120+
<%= react_component("HelloWorld", @hello_world_props) %>
121+
```
122+
123+
Here you must both configure the pack entry (`hello-world-bundle.js`) and register the component.
124+
125+
---
126+
127+
### Auto-Bundling (with Auto-Registration)
128+
129+
```erb
130+
<%= react_component("HelloWorld", @hello_world_props, auto_load_bundle: true) %>
131+
<%= react_component("HeavyMarkdownEditor", @editor_props, auto_load_bundle: true) %>
132+
```
133+
134+
With `auto_load_bundle: true`, and by placing your "exposed" components in the appropriate directories, React on Rails:
135+
136+
- Automatically finds and bundles your component.
137+
- Automatically registers it for use.
138+
- Eliminates the need for manual pack configuration.
139+
140+
See [Auto-Bundling: File-System-Based Automated Bundle Generation][./guides/auto-bundling-file-system-based-automated-bundle-generation.md]
141+
142+
Exposing your component in this way allows you to reference the component from a Rails view. You can expose as many components as you like, but their names must be unique. See below for the details of how you expose your components via the React on Rails Webpack configuration. You may call `ReactOnRails.register` many times.
143+
144+
- `@some_props` can be either a hash or JSON string. This is an optional argument assuming you do not need to pass any options (if you want to pass options, such as `prerender: true`, but you do not want to pass any properties, simply pass an empty hash `{}`). This will make the data available in your component:
106145

107146
- This is what your HelloWorld.js file might contain. The railsContext is always available for any parameters that you _always_ want available for your React components. It has _nothing_ to do with the concept of the [React Context](https://react.dev/reference/react/useContext). See [Render-Functions and the RailsContext](./guides/render-functions-and-railscontext.md) for more details on this topic.
108147

@@ -120,22 +159,15 @@ issue.
120159
};
121160
```
122161

123-
See the [View Helpers API](./api/view-helpers-api.md) for more details on `react_component` and its sibling function `react_component_hash`.
124-
125-
## Globally Exposing Your React Components
162+
## What Happens Next?
126163

127-
For the React on Rails view helper `react_component` to use your React components, you will have to **register** them in your JavaScript code.
164+
The generator set up the following:
128165

129-
Use modules just as you would when using Webpack and React without Rails. The difference is that instead of mounting React components directly to an element using `React.render`, you **register your components to ReactOnRails and then mount them with helpers inside of your Rails views**.
166+
1. Component directory: `app/javascript/bundles/HelloWorld`
167+
2. Rails integration for rendering this component in a Rails view
168+
3. Webpack configuration for building your JavaScript bundle
130169

131-
This is how to expose a component to the `react_component` view helper.
132-
133-
```javascript
134-
// app/javascript/packs/hello-world-bundle.js
135-
import HelloWorld from '../components/HelloWorld';
136-
import ReactOnRails from 'react-on-rails';
137-
ReactOnRails.register({ HelloWorld });
138-
```
170+
![Basic Hello World Example](../images/bundle-splitting-hello-world.png)
139171

140172
#### Different Server-Side Rendering Code (and a Server-Specific Bundle)
141173

@@ -145,7 +177,7 @@ Another way is to use a separate Webpack configuration file that can use a diffe
145177

146178
For details on techniques to use different code for client and server rendering, see: [How to use different versions of a file for client and server rendering](https://forum.shakacode.com/t/how-to-use-different-versions-of-a-file-for-client-and-server-rendering/1352). (_Requires creating a free account._)
147179

148-
## Specifying Your React Components: Register directly or use render-functions
180+
## Specifying Your React Components
149181

150182
You have two ways to specify your React components. You can either register the React component (either function or class component) directly, or you can create a function that returns a React component, which we using the name of a "render-function". Creating a render-function allows you to:
151183

@@ -200,3 +232,24 @@ For details on using react_component_hash with react-helmet, see [our react-helm
200232

201233
React on Rails provides an option for automatic conversions of Rails `*.yml` locale files into `*.json` or `*.js`.
202234
See the [How to add I18n](./guides/i18n.md) for a summary of adding I18n.
235+
236+
## More Reading
237+
238+
Depending on your goals, here's a progression of what to do next:
239+
240+
1. **[View Helpers API](./api/view-helpers-api.md)** - for more options of the `react_component` method.
241+
2. **[Tutorial](./guides/tutorial.md)** - Comprehensive walkthrough of features with a real app.
242+
3. **[Configuration](./guides/configuration.md)** - Details on every possible option you can configure.
243+
4. **[Migration Guide](./guides/upgrading-react-on-rails.md)** - Upgrade advice for each version.
244+
245+
---
246+
247+
## Additional Resources
248+
249+
### Rails/React Integration Options
250+
251+
- **[Rails + Webpack Comparison](./guides/rails-webpacker-react-integration-options.md)**
252+
253+
### JavaScript/TypeScript Module System
254+
255+
- See the official [Shakapacker documentation](https://github.com/shakacode/shakapacker) for more details regarding this topic.

docs/guides/file-system-based-automated-bundle-generation.md renamed to docs/guides/auto-bundling-file-system-based-automated-bundle-generation.md

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File-System-Based Automated Bundle Generation
1+
# Auto-Bundling: File-System-Based Automated Bundle Generation
22

33
To use the automated bundle generation feature introduced in React on Rails v13.1.0, please upgrade to use [Shakapacker v6.5.1](https://github.com/shakacode/shakapacker/tree/v6.5.1) at least. If you are currently using Webpacker, please follow the migration steps available [v6 upgrade](https://github.com/shakacode/shakapacker/blob/master/docs/v6_upgrade.md). Then upgrade to Shakapacker 7 using [v7 upgrade](https://github.com/shakacode/shakapacker/blob/master/docs/v7_upgrade.md) guide.
44

@@ -602,20 +602,6 @@ As of version 13.3.4, bundles inside directories that match `config.components_s
602602
- **Minification**: Some code might break during minification - check console for errors
603603
- **Environment**: Use `bin/dev prod` to test production-like assets locally
604604

605-
#### 8. Installation order issues
606-
607-
**Problem**: React on Rails installation fails or behaves unexpectedly.
608-
609-
**Solutions**:
610-
611-
- **Correct order**: Always install Shakapacker BEFORE React on Rails
612-
```bash
613-
bundle add shakapacker
614-
rails shakapacker:install
615-
bundle add react_on_rails
616-
rails generate react_on_rails:install
617-
```
618-
- If you installed in wrong order, uninstall and reinstall in correct sequence
619605

620606
### Debug Mode
621607

lib/tasks/generate_packs.rake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ namespace :react_on_rails do
8383
puts Rainbow(" • Keep only the client/server specific files " \
8484
"(Component.client.jsx, Component.server.jsx)").white
8585
puts Rainbow(" • See: https://www.shakacode.com/react-on-rails/docs/guides/" \
86-
"file-system-based-automated-bundle-generation.md").cyan
86+
"auto-bundling-file-system-based-automated-bundle-generation.md").cyan
8787

8888
when /Cannot find component/
8989
puts Rainbow(" • Check that your component file exists in the expected location").white
@@ -118,7 +118,7 @@ namespace :react_on_rails do
118118
puts ""
119119
puts Rainbow("📚 DOCUMENTATION:").magenta.bold
120120
puts Rainbow(" • File-system based components: https://www.shakacode.com/react-on-rails/docs/" \
121-
"guides/file-system-based-automated-bundle-generation.md").cyan
121+
"guides/auto-bundling-file-system-based-automated-bundle-generation.md").cyan
122122
puts Rainbow(" • Component registration: https://www.shakacode.com/react-on-rails/docs/").cyan
123123
puts Rainbow("=" * 80).red
124124
end

0 commit comments

Comments
 (0)