一、子组件调用父组件,采用props的方式进行调用和赋值,在父组件中设置相关属性值或者方法,子组件经过props的方式进行属性赋值或者方法调用;html
二、父组件调用子组件,采用refs的方式进行调用,须要父组件在调用子组件的时候,添加ref属性,并进行惟一命名,在父组件中便可调用;react
var ButtonComment = React.createClass({ getInitialState: function () { return {count:0}; }, //点击发宝刀。。。 sendSword: function () { var newCount = this.state.count + 1; this.setState({ count:newCount }); //经过props调用父组件的方法 this.props.getSwordCount(newCount ); }, render: function () { return ( <button onClick={this.sendSword}>{this.props.buttonName}</button> ); } });
点击按钮,将会调用sendWord方法,更改count的状态,并调用父组件的方法getSwordCount,这时将会从新渲染页面,若是不想从新渲染请重写方法shouldComponentUpdate: function (nextProps,nextState){}并返回false便可。jquery
var ImDaddyComponent = React.createClass({ getInitialState: function () { return {sendCount:0}; }, getSwordCount: function (newCount) { this.setState({sendCount:newCount}); }, render: function () { return ( <div> <ButtonComment getSwordCount={this.getSwordCount} buttonName="儿子送宝刀"/> <p> 父子俩共送{this.state.sendCount}把宝刀!!! </p> </div> ); } });
React.render( <ImDaddyComponent />, document.getElementById('index-0331-0011') );
以上就完成了子组件调用父组件的属性及方法。this
var ImDaddyComponent = React.createClass({ getInitialState: function () { return {sendCount:0}; }, //经过refs方式调用子组件的方法sendSword sendSword: function () { this.refs.getSwordButton.sendSword(); }, getSwordCount: function () { //经过refs方式调用子组件的属性count this.setState({sendCount:this.refs.getSwordButton.state.count + 1}); }, render: function () { return ( <div> //此处须要定义ref属性,且值惟一 <ButtonComment ref="getSwordButton" getSwordCount={this.getSwordCount} buttonName="儿子送宝刀"/> <button onClick={this.sendSword}>经过老爸送宝刀</button> <p> 父子俩共送{this.state.sendCount}把宝刀!!! </p> </div> ); } });
以上,就完成父组件调用子组件。spa
<!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>