Skip to content

Commit 1846c60

Browse files
committedFeb 26, 2022
init react
1 parent 0b98c23 commit 1846c60

File tree

15 files changed

+169
-13
lines changed

15 files changed

+169
-13
lines changed
 

‎app/assets/stylesheets/home.scss

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the Home controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: https://sass-lang.com/

‎app/controllers/home_controller.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class HomeController < ApplicationController
2+
def index
3+
end
4+
end

‎app/helpers/application_helper.rb

+7
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
module ApplicationHelper
2+
def react_component(component_name, props = {})
3+
print(props.to_json)
4+
tag.div(data: {
5+
react_component: component_name,
6+
props: props.to_json
7+
}) { '' }
8+
end
29
end

‎app/helpers/home_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module HomeHelper
2+
end

‎app/javascript/hello_react.jsx

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import React from 'react'
2+
3+
export default props => <div>Hello {props.name}!</div>

‎app/javascript/mount.tsx

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
4+
export default function mount(components = {}) {
5+
document.addEventListener('DOMContentLoaded', () => {
6+
const mountPoints = document.querySelectorAll('[data-react-component]');
7+
8+
mountPoints.forEach((mountPoint) => {
9+
const dataset = (mountPoint as HTMLElement).dataset;
10+
const componentName = dataset['reactComponent'];
11+
const Component = components[componentName];
12+
13+
if (Component) {
14+
const props = JSON.parse(dataset['props']);
15+
ReactDOM.render(<Component {...props} />, mountPoint);
16+
} else {
17+
console.warn(
18+
'WARNING: No component found for: ',
19+
dataset.reactComponent,
20+
components
21+
);
22+
}
23+
});
24+
});
25+
}

‎app/javascript/packs/application.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
// This file is automatically compiled by Webpack, along with any other files
2-
// present in this directory. You're encouraged to place your actual application logic in
3-
// a relevant structure within app/javascript and only use these pack files to reference
4-
// that code so it'll be compiled.
5-
61
import Rails from "@rails/ujs"
72
import Turbolinks from "turbolinks"
83
import * as ActiveStorage from "@rails/activestorage"
4+
import mount from '../mount';
95
import "channels"
106

7+
// React Components
8+
import Hello from "../hello_react"
9+
1110
Rails.start()
1211
Turbolinks.start()
1312
ActiveStorage.start()
13+
14+
mount({Hello})

‎app/views/home/index.html.erb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>Home</h1>
2+
<%= react_component "Hello", { name: "Test" } %>

‎babel.config.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ module.exports = function(api) {
2222
{
2323
targets: {
2424
node: 'current'
25-
}
26-
}
25+
},
26+
modules: 'commonjs'
27+
},
28+
'@babel/preset-react'
2729
],
2830
(isProductionEnv || isDevelopmentEnv) && [
2931
'@babel/preset-env',
@@ -35,7 +37,16 @@ module.exports = function(api) {
3537
exclude: ['transform-typeof-symbol']
3638
}
3739
],
38-
['@babel/preset-typescript', { 'allExtensions': true, 'isTSX': true }]
40+
['@babel/preset-typescript',
41+
{ 'allExtensions': true, 'isTSX': true }
42+
],
43+
[
44+
'@babel/preset-react',
45+
{
46+
development: isDevelopmentEnv || isTestEnv,
47+
useBuiltIns: true
48+
}
49+
]
3950
].filter(Boolean),
4051
plugins: [
4152
'babel-plugin-macros',
@@ -69,7 +80,9 @@ module.exports = function(api) {
6980
[
7081
'@babel/plugin-transform-runtime',
7182
{
72-
helpers: false
83+
helpers: false,
84+
regenerator: true,
85+
corejs: false
7386
}
7487
],
7588
[

‎config/routes.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Rails.application.routes.draw do
2+
root to: "home#index"
3+
24
post "/graphql", to: "graphql#execute"
3-
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
45
end

‎config/webpacker.yml

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ default: &default
3333
- .woff2
3434

3535
extensions:
36+
- .jsx
3637
- .tsx
3738
- .ts
3839
- .mjs

‎package.json

+3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
"name": "books",
33
"private": true,
44
"dependencies": {
5+
"@babel/preset-react": "^7.16.7",
56
"@babel/preset-typescript": "^7.16.7",
67
"@rails/actioncable": "^6.0.0",
78
"@rails/activestorage": "^6.0.0",
89
"@rails/ujs": "^6.0.0",
910
"@rails/webpacker": "5.4.3",
11+
"react": "^17.0.2",
12+
"react-dom": "^17.0.2",
1013
"turbolinks": "^5.2.0",
1114
"typescript": "^4.5.5",
1215
"webpack": "^4.46.0",
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require "test_helper"
2+
3+
class HomeControllerTest < ActionDispatch::IntegrationTest
4+
test "should get index" do
5+
get home_index_url
6+
assert_response :success
7+
end
8+
end

‎tsconfig.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"compilerOptions": {
3+
"jsx": "react",
34
"declaration": false,
45
"emitDecoratorMetadata": true,
56
"experimentalDecorators": true,
@@ -21,6 +22,4 @@
2122
"public"
2223
],
2324
"compileOnSave": false,
24-
"jsx": "react"
2525
}
26-

‎yarn.lock

+85-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)