this.state 組件免不了要與用戶互動,React 的一大創新,就是將組件看成是一個狀態機,一開始有一個初始狀態,然後用戶互動,導致狀態變化,從而觸發重新渲染 UI http://www.ruanyifeng.com/blog/2015/03/react.html demo8
Created
October 17, 2016 02:14
-
-
Save JacobHsu/19847b5c1a4dad8f6ace6d786975918b to your computer and use it in GitHub Desktop.
Hello World React
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 id="example"></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
var LikeButton = React.createClass({ | |
getInitialState: function() { | |
return {liked: false}; | |
}, | |
handleClick: function(event) { | |
this.setState({liked: !this.state.liked}); | |
}, | |
render: function() { | |
var text = this.state.liked ? 'like' : 'haven\'t liked'; | |
return ( | |
<p onClick={this.handleClick}> | |
You {text} this. Click to toggle. | |
</p> | |
); | |
} | |
}); | |
ReactDOM.render( | |
<LikeButton />, | |
document.getElementById('example') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment