我想从React元素的实例中调用React组件公开的方法。
例如,在此jsfiddle中。我想alertMessage
从HelloElement
参考中调用该方法。
有没有一种方法可以实现而无需编写其他包装程序?
编辑(从JSFiddle复制的代码)
<div id="container"></div>
<button onclick="onButtonClick()">Click me!</button>
var onButtonClick = function () {
//call alertMessage method from the reference of a React Element! Something like HelloElement.alertMessage()
console.log("clicked!");
}
var Hello = React.createClass({displayName: 'Hello',
alertMessage: function() {
alert(this.props.name);
},
render: function() {
return React.createElement("div", null, "Hello ", this.props.name);
}
});
var HelloElement = React.createElement(Hello, {name: "World"});
React.render(
HelloElement,
document.getElementById('container')
);