-
-
Notifications
You must be signed in to change notification settings - Fork 631
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
95 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 24 additions & 42 deletions
66
..._on_rails/templates/base/base/app/javascript/bundles/HelloWorld/components/HelloWorld.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,27 @@ | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
import React, { useState} from 'react'; | ||
|
||
export default class HelloWorld extends React.Component { | ||
static propTypes = { | ||
name: PropTypes.string.isRequired, // this is passed from the Rails view | ||
}; | ||
const HelloWorld = (props) => { | ||
const [name, setName] = useState(props.name); | ||
|
||
/** | ||
* @param props - Comes from your rails view. | ||
*/ | ||
constructor(props) { | ||
super(props); | ||
return ( | ||
<div> | ||
<h3> | ||
Hello, {name}! | ||
</h3> | ||
<hr /> | ||
<form > | ||
<label htmlFor="name"> | ||
Say XXXXX to: | ||
</label> | ||
<input | ||
id="name" | ||
type="text" | ||
value={name} | ||
onChange={(e) => setName(e.target.value)} | ||
/> | ||
</form> | ||
</div> | ||
); | ||
}; | ||
|
||
// How to set initial state in ES6 class syntax | ||
// https://reactjs.org/docs/state-and-lifecycle.html#adding-local-state-to-a-class | ||
this.state = { name: this.props.name }; | ||
} | ||
|
||
updateName = (name) => { | ||
this.setState({ name }); | ||
}; | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<h3> | ||
Hello, {this.state.name}! | ||
</h3> | ||
<hr /> | ||
<form > | ||
<label htmlFor="name"> | ||
Say hello to: | ||
</label> | ||
<input | ||
id="name" | ||
type="text" | ||
value={this.state.name} | ||
onChange={(e) => this.updateName(e.target.value)} | ||
/> | ||
</form> | ||
</div> | ||
); | ||
} | ||
} | ||
export default HelloWorld; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters