Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React.js how to pass callbacks to child components?

I would like to pass a callback to a doubly nested component, and while I am able to pass the properties effectively, I can't figure out how to bind the callback to the correct component so that it's triggered. My structure looks like this:

-OutermostComponent     -FirstNestedComponent         -SecondNestedComponent             -DynamicallyGeneratedListItems 

The List Items when clicked should trigger a callback which is the OutermostComponents method "onUserInput", but instead I get "Uncaught Error: Undefined is not a function". I suspect the problem is in how I am rendering the SecondNestedComponent inside the first, and passing it the callback. The code looks something like this:

var OutermostComponent = React.createClass({     onUserInput: //my function,     render: function() {         return (             <div>             //other components                  <FirstNestedComponent                     onUserInput={this.onUserInput}                 />             </div>         );     } });  var FirstNestedComponent = React.createClass({     render: function() {         return (             <div>             //other components                  <SecondNestedComponent                     onUserInput={this.onUserInput}                 />             </div>         );     } }); var SecondNestedComponent = React.createClass({     render: function() {         var items = [];         this.props.someprop.forEach(function(myprop) {             items.push(<DynamicallyGeneratedListItems myprop={myprop} onUserInput={this.props.onUserInput}/>);}, this);         return (             <ul>                 {items}             </ul>         );     } }); 

How do I correctly bind callbacks to the appropriate nested components?

like image 424
bryant Avatar asked Mar 06 '15 23:03

bryant


People also ask

How do you pass data to child component React?

React. js allows us to use props (short form of property) to pass data from parent component to child component. We have to set props value inside the child component, while we embed it to the parent component.

How do you pass property from parent to child in React?

To pass data from a child component to its parent, we can call a parent function from the child component with arguments. The parent function can be passed down to the child as a prop, and the function arguments are the data that the parent will receive.


1 Answers

You are passing this.onUserInput as a property to FirstNestedComponent. Therefore, you should access it in FirstNestedComponent as this.props.onUserInput.

var FirstNestedComponent = React.createClass({     render: function() {         return (             <div>                 <SecondNestedComponent                     onUserInput={this.props.onUserInput}                 />             </div>         );     } }); 
like image 187
Dustin Hayes Avatar answered Oct 07 '22 11:10

Dustin Hayes