Last active
April 26, 2017 10:39
-
-
Save rgbkids/9c9c7c11ca0e4497f4b8 to your computer and use it in GitHub Desktop.
出来る限り短く説明するReact.js入門 ref: http://qiita.com/rgbkids/items/8ec309d1bf5e203d2b19
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="http://fb.me/react-0.13.3.js"></script> | |
<script src="http://fb.me/JSXTransformer-0.13.3.js"></script> | |
</head> | |
<body> | |
<div id="app"></div><!-- ←ここに埋め込まれる --> | |
<!-- 以下、Reactのプログラム --> | |
<!-- (jsxを使っているのでtext/jsxと書く) --> | |
<script type="text/jsx"> | |
var HelloWorld = React.createClass({ | |
// <HelloWorld />をレンダリング(表示) | |
render: function() { | |
return ( | |
<p>Hello!World!</p> | |
); | |
} | |
}); | |
// id='app'に<HelloWorld />を埋め込む(マウント) | |
var m = React.render(<HelloWorld />, document.getElementById('app')); | |
</script> | |
</body> | |
</html> |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="http://fb.me/react-0.13.3.js"></script> | |
<script src="http://fb.me/JSXTransformer-0.13.3.js"></script> | |
</head> | |
<body> | |
<div id="app"></div> | |
<script type="text/jsx"> | |
// 親:<Parent />の定義 | |
var Parent = React.createClass({ | |
// State(※状態は親が管理) | |
// この値はブラウザを閉じたり、リロードするまでは保持される | |
getInitialState: function () { | |
return { | |
textVal: "", | |
children: [] | |
}; | |
}, | |
// State(textVal)を変更 | |
setStateTextVal: function(textVal) { | |
this.setState({ textVal: textVal }); | |
}, | |
// State(children)を変更 | |
setStateChildren: function(textVal) { | |
var textVals = this.state.children.concat(textVal); | |
this.setState({ children: textVals }); | |
}, | |
// <Parent />の表示 | |
// ここで子となる<ChildInput />と<Child />を記述 | |
render: function() { | |
return ( | |
<div> | |
<p>入力してEnterキーを押す</p> | |
<ChildInput onChange={this.setStateTextVal} onSave={this.setStateChildren} /> | |
<Child textVal={this.state.textVal} children={this.state.children} /> | |
</div> | |
); | |
} | |
}); | |
// 子1:<ChildInput />の定義(※props経由で親を参照できる) | |
var ChildInput = React.createClass({ | |
_onChange: function (e) { | |
this.props.onChange(e.target.value); | |
}, | |
_onKeyDown: function (e) { | |
if (e.keyCode === 13) { // Enterキー | |
this.props.onSave(e.target.value); | |
e.target.value = ""; | |
} | |
}, | |
// <ChildInput />の表示 | |
render: function() { | |
return <input type="text" onChange={this._onChange} onKeyDown={this._onKeyDown} />; | |
} | |
}); | |
// 子2:<Child />の定義(※props経由で親を参照できる) | |
var Child = React.createClass({ | |
// <Child />の表示 | |
render: function() { | |
var key = 0; | |
var textVals = this.props.children.map(function (textVal) { | |
// 時間が同じ。つまり、キーが押されるごとに、まとめて再描画されていることに注目 | |
//(サーバーサイドっぽいと言われる所以) | |
var date = new Date().toString(); | |
return <li key={key++}>{key}.{textVal}({date})</li>; | |
}); | |
return ( | |
<div> | |
<p>{this.props.textVal}</p> | |
<ul>{textVals}</ul> | |
</div> | |
); | |
} | |
}); | |
// id='app' に <Parent />を表示する(マウント) | |
var m = React.render(<Parent />, document.getElementById('app')); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment