我想通过 React Router 将函数传递给子组件。我尝试了以下方法,但似乎不起作用。
class App extends Component {
constructor(props) {
super(props)
}
render() {
return (
<div className="App">
<div className="content">
<div className="centered-wrapper">
<Switch>
<Route exact path="/" component={Welcome} />
<Route path="/life" render={props => <Life sayHello = {this.sayHello()} />} />
</Switch>
</div>
</div>
);
}
}
export default App;
我想打电话sayHello()
在Life组件中如下:
<div className="hello">{ this.props.sayHello() } I'm <Link to="/life">Marco</Link>! Welcome to my hood.</div>
Replace:
<Route path="/life" render={props => <Life sayHello = {this.sayHello()} />} />
with
<Route path="/life" render={props => <Life sayHello = {this.sayHello} />} />
错误 props 收到调用的结果sayHello()
函数而不是函数本身。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)