react-state的问题
1、setState是异步的,在调用了setState之后,this.state的值并没有立即改变,可能还是使用原来的值。
<!DOCTYPE html>
<html lang="en">
<head>
<Meta charset="UTF-8">
<title>测试state</title>
<script type="text/javascript" src="bower_components/react/react.js"></script>
<script type="text/javascript" src="bower_components/react/JSXTransformer.js"></script>
</head>
<body>
<div id="app"></div>
<!-- 遗留问题!!! -->
<script type="text/jsx"> var MessageBox = React.createClass({ handleClick:function(){ this.setState({ clickCount: this.state.clickCount + 1,clickContent: '你一共点击了'+this.state.clickCount+'次' }); },getInitialState: function(){ return { clickCount: 0,clickContent: '你还没有点击哦' } },render: function(){ return ( <div> <h3>点击下面按钮:</h3> <button onClick={this.handleClick}>点击我</button> <div>{this.state.clickContent}</div> </div> ); } }); React.render( <MessageBox />,document.getElementById('app') ); </script>
</body>
</html>
原文链接:https://www.f2er.com/react/301843.html