Last active
September 22, 2016 07:41
-
-
Save endam/db2d47bf51b0c79f8316f1a03490fd9f to your computer and use it in GitHub Desktop.
React.jsの知っておいて損はないTips ref: http://qiita.com/endam/items/1bde821c4b29f9b663da
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
// エラーになる | |
render(){ | |
return( | |
<div>aaa</div> | |
<div>bbb</div> | |
); | |
} | |
// 正常 | |
render(){ | |
return( | |
<div> | |
<div>aaa</div> | |
<div>bbb</div> | |
</div> | |
); | |
} |
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
render() { | |
if (!this.props.isShow) { | |
return null; // もしくはreturn false; | |
} | |
return ( | |
<div> | |
表示します | |
</div> | |
); | |
} |
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
<div className="header {showClass}"> |
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
const headerClass = "header " + (isShow ? "show" : "hide"); | |
... | |
<div className={headerClass}> |
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
<div className={`header ${isShow ? "show" : "hide"}`}> |
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
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> | |
); | |
} |
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
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> | |
); | |
} |
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
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 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.props.isShow && <div>テスト</div>} |
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.props.isShow ? <div>テスト</div> : null} |
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
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> | |
); | |
} |
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
<div className="header"> |
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
<div className={showClass}> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment