Skip to content

Instantly share code, notes, and snippets.

@endam
Last active September 22, 2016 07:41
Show Gist options
  • Save endam/db2d47bf51b0c79f8316f1a03490fd9f to your computer and use it in GitHub Desktop.
Save endam/db2d47bf51b0c79f8316f1a03490fd9f to your computer and use it in GitHub Desktop.
React.jsの知っておいて損はないTips ref: http://qiita.com/endam/items/1bde821c4b29f9b663da
// エラーになる
render(){
return(
<div>aaa</div>
<div>bbb</div>
);
}
// 正常
render(){
return(
<div>
<div>aaa</div>
<div>bbb</div>
</div>
);
}
render() {
if (!this.props.isShow) {
return null; // もしくはreturn false;
}
return (
<div>
表示します
</div>
);
}
<div className="header {showClass}">
const headerClass = "header " + (isShow ? "show" : "hide");
...
<div className={headerClass}>
<div className={`header ${isShow ? "show" : "hide"}`}>
render() {
const label = this.props.isShow ? <div>テスト</div> : null;
const commentNodes = this.props.data.map((comment) => {
return (
<Comment author={comment.author}>
{comment.text}
</Comment>
);
});
return (
<div>
{label}
<div className="commentList">
{commentNodes}
</div>
</div>
);
}
render() {
return (
<div className="commentList">
{(() => {
if (this.props.isShow) {
return <div>テスト</div>;
}
})()}
{(() => {
return (
this.props.data.map((comment)=> {
return <Comment author={comment.author}>
{comment.text}
</Comment>
})
);
})()}
</div>
);
}
render() {
return (
<div className="commentList">
{this.props.isShow && <div>テスト</div>}
{this.props.data.map((comment)=> {
return (
<Comment author={comment.author}>
{comment.text}
</Comment>
);
})}
</div>
);
}
{this.props.isShow && <div>テスト</div>}
{this.props.isShow ? <div>テスト</div> : null}
onKeyPress(e) {
console.log(`onKeyPress KeyCode:${e.charCode}`)
}
onKeyDown(e) {
console.log(`onKeyDown KeyCode:${e.keyCode}`)
}
onKeyUp(e) {
console.log(`onKeyUp KeyCode:${e.keyCode}`)
}
render() {
return (
<div>
<form onSubmit={this.onSubmit}>
<input type="text" onKeyDown={this.onKeyDown} onKeyPress={this.onKeyPress} onKeyUp={this.onKeyUp}/>
<button type="submit">登録</button>
</form>
</div>
);
}
<div className="header">
<div className={showClass}>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment