Reactjs 通过路由器传递函数
我想通过React路由器将函数传递给子组件。我尝试了以下方法,但似乎不起作用Reactjs 通过路由器传递函数,reactjs,react-router,Reactjs,React Router,我想通过React路由器将函数传递给子组件。我尝试了以下方法,但似乎不起作用 class App extends Component { constructor(props) { super(props) } render() { return ( <div className="App"> <div className="content"> <div className="cen
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;
替换:
<Route path="/life" render={props => <Life sayHello = {this.sayHello()} />} />
}/>
与
}/>
错误道具接收调用
sayHello()
函数的结果,而不是函数本身。不是传递函数,而是调用它并将返回的结果作为道具传递
sayHello // function object
sayHello() // function call, evaluates to return
删除括号:
render={props => <Life sayHello={this.sayHello} />}
有了这些信息,你可能自己就发现了问题,或者让任何试图帮助的人都能更清楚地了解问题:)
sayHello // function object
sayHello() // function call, evaluates to return
render={props => <Life sayHello={this.sayHello} />}
Uncaught TypeError: undefined is not a function