【发布时间】:2020-05-09 08:16:36
【问题描述】:
当用户单击App 组件中的<button> 时,我试图调用我的Modal 组件的show 方法,但它不起作用。
我使用ref 从App 组件访问Modal 组件。
class Modal extends React.Component {
constructor(props) {
super(props);
this.show = this.show.bind(this);
}
show() {
console.log('show');
}
render() {
return (
<div>...</div>
);
}
}
class App extends Component {
constructor(props) {
super(props);
this.modalRef = React.createRef();
}
render() {
return (
<div>
<Modal ref={this.modalRef}/>
<button id="myBtn" onClick={ this.modalRef.show }>
Call show modal method
</button>
</div>
);
}
}
【问题讨论】:
标签: reactjs event-handling modal-dialog react-props react-ref