I am attempting to pass a function down to a child component in a react router. I've figured out how to use render to send my props through and am using withRouter to make them available to my current state.
I cannot get the function to pass through to another function defined by the component receiving it.
Sample code below - kinda chopped up
// generic class with a router attempting to pass a function to a child
class App extends Component{
...
modalLogin = () => {
console.log('modal login called')
this.setState({modalLoginTrigger:true})
}
render(){
render(
<router>
<switch>
<Router ....more paths>
<Route path='/product/:_id' render={(props) =>(<ProductPage onModalLogin={this.modalLogin} />)}/>
</siwtch>
</router>)
ProductPage - receiving all of the parent props correctly, but not connecting the addToBinder() function to this.props.onModalLogin
class ProductPage extends Component {
addToBinder(){
this.props.onModalLogin()
}
render() {
<div>
<a className="cb_binder-link__add-remove col-xs-6" onClick={this.addToBinder} href="#">
</div>
}
export default withRouter(ProductPage) ;
When my onclick function in my anchor is called, I am getting a
TypeError: Cannot read property 'props' of undefined This is coming from addToBinder()
When I call the onModalLogin function from the render itself, it works.
class ProductPage extends Component {
addToBinder(){
this.props.onModalLogin()
}
render() {
<div>
<a className="cb_binder-link__add-remove col-xs-6" onClick={this.props.onModalLogin} href="#">
</div>
}
export default withRouter(ProductPage) ;
I want to be able to conduct business logic in addToBinder before calling onModalLogin - e.g. check if the user is logged in, prompt for a login if not. How do i get this to connect?