@@ -39,6 +39,7 @@ Example app code available here: https://github.com/BookOfGreg/react-rails-examp
39
39
- [ Camelize Props] ( #camelize-props )
40
40
- [ Upgrading] ( #upgrading )
41
41
- [ 2.3 to 2.4] ( #23-to-24 )
42
+ - [ Common Errors] ( #common-errors )
42
43
- [ Related Projects] ( #related-projects )
43
44
- [ Contributing] ( #contributing )
44
45
@@ -56,47 +57,61 @@ https://github.com/reactjs/React-Rails/wiki
56
57
57
58
[ Webpacker] ( https://github.com/rails/webpacker ) integrates modern JS tooling with Rails.
58
59
59
- Add ` webpacker ` and ` react-rails ` to your gemfile and run the installers:
60
+ 1 ) Create a new Rails app:
61
+ ```
62
+ $ rails new my-app
63
+ $ cd my-app
64
+ ```
65
+
66
+ 2 ) Add ` webpacker ` and ` react-rails ` to your gemfile
67
+ ```
68
+ gem 'webpacker'
69
+ gem 'react-rails'
70
+ ```
60
71
72
+ 3 ) Now run the installers:
61
73
```
62
74
$ bundle install
63
75
$ rails webpacker:install # OR (on rails version < 5.0) rake webpacker:install
64
76
$ rails webpacker:install:react # OR (on rails version < 5.0) rake webpacker:install:react
65
77
$ rails generate react:install
66
78
```
67
-
68
79
This gives you:
69
80
70
81
- ` app/javascript/components/ ` directory for your React components
71
82
- [ ` ReactRailsUJS ` ] ( #ujs ) setup in ` app/javascript/packs/application.js `
72
83
- ` app/javascript/packs/server_rendering.js ` for [ server-side rendering] ( #server-side-rendering )
73
84
74
- Link the JavaScript pack in Rails view using ` javascript_pack_tag ` [ helper] ( https://github.com/rails/webpacker#usage ) , for example:
75
-
85
+ 4 ) Link the JavaScript pack in Rails view using ` javascript_pack_tag ` [ helper] ( https://github.com/rails/webpacker#usage ) , for example:
76
86
```
77
- <!-- application.html.erb -->
87
+ <!-- application.html.erb in Head tag below turbolinks -->
78
88
<%= javascript_pack_tag 'application' %>
79
89
```
80
90
81
- Generate your first component:
82
-
91
+ 5 ) Generate your first component:
83
92
```
84
93
$ rails g react:component HelloWorld greeting:string
85
94
```
86
95
87
- Your component is added to ` app/javascript/components/ ` by default.
88
-
89
- You can also generate your component in a subdirectory:
90
-
96
+ 6 ) You can also generate your component in a subdirectory:
91
97
```
92
98
$ rails g react:component my_subdirectory/HelloWorld greeting:string
93
99
```
100
+ Note: Your component is added to ` app/javascript/components/ ` by default.
94
101
95
- [ Render it in a Rails view] ( #view-helper ) :
96
102
97
- ``` erb
98
- <%= react_component("HelloWorld", { greeting: "Hello" }) %>
103
+ 7 ) [ Render it in a Rails view] ( #view-helper ) :
104
+
105
+ ```
106
+ <!-- erb: paste this in view -->
107
+ <%= react_component("HelloWorld", { greeting: "Hello from react-rails." }) %>
108
+ ```
109
+
110
+ 8 ) Lets Start the app:
111
+ ```
112
+ $ rails s
99
113
```
114
+ output: greeting: Hello from react-rails", inspect webpage in your browser too see change in tag props.
100
115
101
116
The component name tells ` react-rails ` where to load the component. For example:
102
117
@@ -574,6 +589,31 @@ For the vast majority of cases this will get you most of the migration:
574
589
- add ` import PropTypes from 'prop-types' ` (Webpacker only)
575
590
- re-run ` bundle exec rails webpacker:install:react ` to update npm packages (Webpacker only)
576
591
592
+ ## Common Errors
593
+ 1 ) While using installers.(rails webpacker:install: react && rails webpacker: install )
594
+ Error:
595
+ ```
596
+ public/packs/manifest.json. Possible causes:
597
+ 1. You want to set webpacker.yml value of compile to true for your environment
598
+ unless you are using the `webpack -w` or the webpack-dev-server.
599
+ 2. webpack has not yet re-run to reflect updates.
600
+ 3. You have misconfigured Webpacker's config/webpacker.yml file.
601
+ 4. Your webpack configuration is not creating a manifest.
602
+ or
603
+ yarn: error: no such option: --dev
604
+ ERROR: [Errno 2] No such file or directory: 'add'
605
+ ```
606
+ Fix: Try updating yarn package.
607
+ ```
608
+ sudo apt remove cmdtest
609
+ sudo apt remove yarn
610
+ curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
611
+ echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
612
+ sudo apt-get update && sudo apt-get install yarn
613
+
614
+ yarn install
615
+ ```
616
+
577
617
## Related Projects
578
618
579
619
- [ webpacker-react] ( https://github.com/renchap/webpacker-react ) : Integration of React with Rails utilizing Webpack with Hot Module Replacement (HMR).
0 commit comments