可以從外部調用反應組件嗎? 例如來自外部的呼叫反應組件
HTML
<div id='react-app'></div>
<button onClick=callReactModal()>PressME</button>
我的分量,我想調用方法
let callReactModal = function() {
console.log('clicked');
//Navigation.sayHello();
}
class Navigation extends React.Component<any, any> {
constructor(props:any){
super(props);
this.state = {
language: {
lang: cookie.load('lang') || ''
}
};
this.click = this.click.bind(this);
}
sayHello =() => {
alert("Hello");
}
}
我不得不從另一個組件調用模態,但我不知道如何實現那。
試圖調用方法,更新state
類和獲取Warning: setState(...): Can only update a mounted or mounting component.
它需要開放模式(使用語義UI),它採用了先進
handleOpen = (e) => this.setState({
modalOpen: true,
})
modalPart
方法
<Modal size='small' open={this.state.modalOpen} onClose={this.handleClose} trigger={<a className="btn btn-base" onClick={this.handleOpen}>Login</a>} closeIcon='close'>
感謝幫幫我!
你需要定義sayHello'靜態',但問題是爲什麼? – juancab
告訴我'未捕獲的ReferenceError:callReactModal沒有被定義' –
看在這[[fiddle](https://jsfiddle.net/odzck2a3/) – juancab