前端之家收集整理的这篇文章主要介绍了
React 组件之间 事件调用(子组件调用父亲组件),
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<!DOCTYPE html>
<html>
<head>
<script src="build/react.js"></script>
<script src="build/react-dom.js"></script>
<script src="build/browser.min.js"></script>
</head>
<body>
<div id="example"></div>
<script type="text/babel">
var HelloMessage = React.createClass({
getDragonKillingSword: function(){
alert("OK");
},render: function() {
return <div> <h1>Hello {this.props.name}</h1> <button onClick={this.props.clickCallback}>OK</button></div>
}
});
var ImDaddyComponent = React.createClass({
getDS: function(){
//父组件调用子主键进行通信
this.refs.getSwordButton.getDragonKillingSword();
},render: function(){
return (
<div>
<HelloMessage name="John" ref="getSwordButton" clickCallback={this.getDS} />
<button onClick={this.getDS}>OK2</button>
</div>
);
}
});
ReactDOM.render(
<ImDaddyComponent />,document.getElementById('example')
);
</script>
</body>
</html>