如何在子React组件中调用父方法?
- 论坛
- 如何在子React组件中调用父方法?
19 浏览
如何在子React组件中调用父方法?
我有一个父组件和一个子组件,我想在子组件中调用父组件的方法,像这样:
import Parent from './parent.js'; class Child extends React.Component { constructor(props) { super(props); }; click() { Parent.someMethod(); } render() { Hello Child onClick={this.click}> } } class Parent extends React.Component { constructor(props) { super(props); }; someMethod() { console.log('bar'); } render() { Hello Parent> } }
这会返回一个错误消息:
Uncaught TypeError: _Parent2.default.someMethod is not a function
如何在子组件中调用父组件的方法?