当用户单击组件中的 时,我试图调用show
我的组件的方法,但它不起作用。Modal
<button>
App
我使用 a从组件ref
访问组件。Modal
App
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>
);
}
}