码迷,mamicode.com
首页 > 其他好文 > 详细

React State

时间:2016-10-27 15:07:40      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:tin   charset   java   div   button   content   blog   doctype   char   

1.getInitialState:设置初始State,返回Object

2.this.setState():改变State

3.都在creatClass内创建类

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,
    minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <script src="lib/react.js"></script>
    <script src="lib/react-dom.js"></script>
    <script src="lib/browser.min.js"></script>
    <title></title>
</head>
<body>
<div id="example"></div>

</body>
<script type="text/babel">
    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 ? ‘喜欢‘ : ‘不喜欢‘;
            return (
                    <p onClick={this.handleClick}>
                    你<b>{text}</b>我。点我切换状态。
                </p>
            );
        }
    });

    ReactDOM.render(
            <LikeButton />,
            document.getElementById(‘example‘)
    );
</script>
</html>

  

React State

标签:tin   charset   java   div   button   content   blog   doctype   char   

原文地址:http://www.cnblogs.com/carlyin/p/6003976.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!