I have been playing with react fairly successfully, however I am a little stumped with how to do this. I am pretty sure I know how to work around it, but not sure if its the best way.
I’ve got a main component which makes up the user interface, with a small dumb component which represents table rows of data.
I want to be able to click on the table row, and have an action perform in the main component. I am already passing all the right information into the dumb component including the function call, I just can’t get the scope of that row into the function… I am either missing something simple, or I am barking up the wrong tree.
In the main component, this is how I call the dumb component. In the main render() function I have {this.listTrades()} which then builds the table content. The table and all the table data work perfectly!
In the doSomething() function I just have no idea where to get the data from… I can’t work out where it is, both e and this are objects but they don’t seem to have the props from the UI component.
I’m guessing this is actually onCLick={doSomething} in which case what you need is onClick={doSomething.bind(this)}
Functional stateless components don’t do autobinding on events (neither do ES6 class based components). You also could do: onClick={e => this.doSomething(e)}
With either of those, this will be your component in your handler.
so that is effectively ES6 shorthand for an anonymous function as the onClick function, which just calls the function passed to the dumb component with trade as the argument. so simple.
I wasn’t sure how complex you could make the bits inside the {}… thats cool.