react 父子组件之间的通信和函数调用
【react】父组件向子组件传值
父向子是用props,然后再子那边有一个监听函数
componentWillReceiveProps:function(nextProps){ this.setState({ visible:nextProps.visible,item:nextProps.item,value:nextProps.value,version:nextProps.version }); }, 父类调用子类的函数 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script src="../../dist/react/react.js"></script> <script src="../../dist/react/JSXTransformer.js"></script> <script src="../../dist/jquery/jquery.min.js"></script> <!--如下的这种引用方式是不正确的,必须使用上面的引用方式--> <!--<script src="../../dist/react/JSXTransformer.js"/>--> </head> <body> <div id="index-0331-0011"></div> <script type="text/jsx"> var ButtonComment = React.createClass({ getInitialState: function () { return {count:0}; },sendSword: function () { var newCount = this.state.count + 1; this.setState({count:this.state.count + 1}); this.props.getSwordCount(); },render: function () { return ( <button onClick={this.sendSword}>{this.props.buttonName}</button> ); } }); var ImDaddyComponent = React.createClass({ getInitialState: function () { return {sendCount:0}; },sendSword: function () { this.refs.getSwordButton.sendSword(); },getSwordCount: function () { this.setState({sendCount:this.refs.getSwordButton.state.count + 1}); },render: function () { return ( <div> <ButtonComment ref="getSwordButton" getSwordCount={this.getSwordCount} buttonName="儿子送宝刀"/> <button onClick={this.sendSword}>通过老爸送宝刀</button> <p> 父子俩共送{this.state.sendCount}把宝刀!!! </p> </div> ); } }); React.render( <ImDaddyComponent />,document.getElementById('index-0331-0011') ); </script> </body> </html> |