Skip to content

Instantly share code, notes, and snippets.

@kazuma1989
Last active January 5, 2018 08:27
Show Gist options
  • Save kazuma1989/161aea315dd899af20c220150db02c13 to your computer and use it in GitHub Desktop.
Save kazuma1989/161aea315dd899af20c220150db02c13 to your computer and use it in GitHub Desktop.
React を、サンプルを実装しながら理解する ref: https://qiita.com/kazuma1989/items/410a1c20f4128eb30327
import React, { Component } from 'react';
import { FormApp } from './FormApp';
class App extends Component {
render() {
return (
<FormApp />
);
}
}
export default App;
class App extends Component {
render() {
return (
<div>
<input type="text" />
<button>SEND</button>
</div>
);
}
}
yarn global add create-react-app
create-react-app qiita
cd qiita
yarn start
class App extends React.Component {
render() {
return (
<h1>Hello world</h1>
);
}
}
ReactDOM.render(
<App />,
document.getElementById('root')
);
import { Component } from '@angular/core';
@Component({
selector: 'app-form-app',
template: `
<div>
<input type="text" [(ngModel)]="value">
<button (click)="send()">SEND</button>
<div>{{message}}</div>
</div>
`
})
export class FormAppComponent {
value = '';
message = '';
send() {
const value = this.value;
this.value = '';
this.message = value;
}
}
import React, { Component } from 'react';
export class FormApp extends Component {
constructor(props) {
super(props);
this.state = {
value: '',
message: ''
};
this.handleInput = this.handleInput.bind(this);
this.send = this.send.bind(this);
}
handleInput({ target: { value } }) {
this.setState({
value
});
}
send() {
const { value } = this.state;
this.setState({
value: '',
message: value
});
}
render() {
return (
<div>
<input type="text" value={this.state.value} onChange={this.handleInput} />
<button onClick={this.send}>SEND</button>
<div>{this.state.message}</div>
</div>
);
}
}
export class FormApp extends Component {
constructor(props) {
super(props);
this.state = {
value: '',
message: ''
};
this.handleInput = this.handleInput.bind(this);
this.send = this.send.bind(this);
}
// ...
render() {
return (
<div>
<input type="text" value={this.state.value} onChange={this.handleInput} />
<button onClick={this.send}>SEND</button>
<div>{this.state.message}</div>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment