A
A
Anton Izmailov2016-03-10 15:21:58
JavaScript
Anton Izmailov, 2016-03-10 15:21:58

React - how to implement parent function call?

Got a question. I just can't get it to work
. I have 2
Parent Components:

<Children 
  info={this.props.info}
  hire={(id) => () => {console.log('testing')}}
/>

And Children Component:
<button onClick={this.handleClick(), this.props.hire(id)} }>Bla</button>

But this construction does not work (the request is not sent to props.hire if the actions in onclick are placed in function().bind(this); - also not an option, both calls stop working if this.props is called directly in handleClick() { } then, too, no reaction.
Has anyone come across this? Is there a solution?
I am supplementing the code with the original.
Here is the Parent:
{this.props.workers && this.props.workers.map( (worker) => {
            return (<div className="large-4 column"><Worker
            info={worker}
            selected={false}
            hire={(id) => () => {console.log('and here ' + id)}}
            /></div>);
          })}

And here is how I call this case in child'e
<div style={{cursor: 'pointer'}} onClick={function bla() {this.handleColor(); hire(5); console.log('function');}.bind(this)}>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Antropov, 2016-03-10
@WapGeaR

Formally, it turns out that you have one level of nesting of functions superfluous, there is an expectation that when you call hire (5) something should fall into the console, then
either

this.handleColor(); 
hire(5)(); 
console.log('function');

Need to use

K
Konstantin Kitmanov, 2016-03-10
@k12th

Everything is correct in Parent, but in Children you can do this:
In render: onClick={this.onClick.bind(this)}
and this method:

onClick () {
    this.props.hire();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question