Skip to content
This repository was archived by the owner on Jan 18, 2023. It is now read-only.

Commit 5ba8a87

Browse files
committed
Webpacker React component example
1 parent 7833f00 commit 5ba8a87

File tree

8 files changed

+100
-27
lines changed

8 files changed

+100
-27
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import React from 'react'
2+
3+
export default class Hello extends React.Component {
4+
constructor(props) {
5+
super(props);
6+
7+
this.state = {
8+
displayedNames: [this.randomName()]
9+
}
10+
11+
this.tryNewName = this.tryNewName.bind(this);
12+
}
13+
14+
tryNewName() {
15+
this.setState(
16+
state => ({
17+
displayedNames: [
18+
...state.displayedNames,
19+
this.randomName()
20+
]
21+
})
22+
)
23+
}
24+
25+
randomName() {
26+
const index = Math.floor(Math.random() * this.props.names.length)
27+
return this.props.names[index]
28+
}
29+
30+
renderNameListElements() {
31+
const displayedNames = this.state.displayedNames
32+
return displayedNames.map(
33+
(name, index) => <li key={index}>...{name}...?</li>
34+
)
35+
}
36+
37+
render() {
38+
return (
39+
<div className="hello-component">
40+
<h3 className="header">Hello...</h3>
41+
<div className="list-container">
42+
<ul>
43+
{this.renderNameListElements()}
44+
</ul>
45+
</div>
46+
<button onClick={this.tryNewName}>
47+
Nope
48+
</button>
49+
</div>
50+
);
51+
52+
}
53+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.hello-component {
2+
border: 1px solid #666;
3+
display: inline flow-root;
4+
min-width: 20em;
5+
padding: 1em;
6+
7+
.header {
8+
margin: 0;
9+
}
10+
11+
ul {
12+
display: inline flow-root;
13+
text-align: left;
14+
15+
li:not(:last-of-type) {
16+
text-decoration: line-through;
17+
}
18+
}
19+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
@import "stylesheets/normalize"; // TRICKY: relative to path added in additional_paths
22
@import "../stylesheets/global";
33
@import "../stylesheets/flair";
4+
@import "../stylesheets/components.scss";

app/javascript/packs/hello_react.jsx

Lines changed: 0 additions & 26 deletions
This file was deleted.

app/javascript/packs/react.jsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from 'react'
2+
import ReactDOM from 'react-dom'
3+
import Hello from '../components/hello_component'
4+
5+
document.addEventListener('turbolinks:load', () => {
6+
const container = document.getElementById('hello-component-container')
7+
ReactDOM.render(<Hello names={['Aaron','AJ','Mark','Mitch','Nate']} />, container)
8+
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "../components/hello_component"

app/views/application/show.html.erb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,21 @@
7575
<button id="add-avatar-button" type="button">I MUST HAVE MORE</button>
7676
<div id="so-many-avatars"></div>
7777
</section>
78+
79+
80+
<section>
81+
<h2>On integrating with React:</h2>
82+
<p>
83+
When I applied for a job at Mutually Human I did my project using React.
84+
I tried not to let on that this was actually a <em>huge</em> gamble for
85+
me; I had started playing with React only a month prior, so I wasn't very
86+
familiar with it yet. It was flexible and fun to play with, though, and
87+
I figured even if I didn't get the job I'd come away having expanded my
88+
knowledge in a new framework. I've barely used React since, but I still
89+
find its architecture quite elegant
90+
</p>
91+
<p>
92+
Here is a React component which will greet you by name (eventually)
93+
</p>
94+
<div id="hello-component-container"></div>
7895
</article>

app/views/layouts/application.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<%= csrf_meta_tags %>
77
<%= csp_meta_tag %>
88

9-
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
9+
<%= javascript_pack_tag 'application', 'react', 'data-turbolinks-track': 'reload' %>
1010
<%= stylesheet_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
1111
</head>
1212

0 commit comments

Comments
 (0)