18

I have a question about passing arguments to React click handlers. I have the following code, but for some reason the node argument is not passed to the toggle function. Shouldn't it? It's defined this way because it's a recursive component.

var Element = React.createClass({

    toggle: function(e,node){

    },

    render: function(){

        var nodes = this.props.children.map(function(n){

                return <Element node={n} text={n.text} children={n.children}  />

        });

        return (
               <span onClick={this.toggle.bind(this,this.props.node)}>{this.props.text}</span>

        );
    }

});
2
  • I'm don't fully understand why you even bind on this.props.node. You can just access this.props.node in the toggle method. Commented Mar 21, 2015 at 21:51
  • I'm not sure if I can follow. You're assigning your the result of your call to map to a nodes variable which you then never use. I don't understand what this code is supposed to do or achieve. Commented Mar 21, 2015 at 22:04

2 Answers 2

23

Function.prototype.bind bind arguments beginning from left. The correct way to receive the node argument is to look for it at the very left position of the argument list:

toggle: function(node, event) {
  console.log('node', node);
  console.log('event', event);
}

See http://jsfiddle.net/8xxfgce7/ for an example.

Sign up to request clarification or add additional context in comments.

7 Comments

You must be doing something different wrong, this works perfectly fine: jsfiddle.net/8xxfgce7
You can totally set the value of a property to a different node, even in a map. The problem here is that you don't actually render the nodes array. There's a whole bunch of things wrong with your code. The canonical way of assigning to this.props.children, is to pass the nodes as children (<Parent><Child /><Child /></Parent>). Assigning to children property like <Parent children={[<Child />, <Child />]} /> may work, but is not the canonical way. Also, once you actually start rendering nodes your code will end in a stack overflow because the recursion never breaks.
I found the problem. It was caused by an unrelated error. My bad
I agree. I accidental removed too much of the code from the actual code to illustrate the real problem. It actually composed as parent - > child components and the model will prevent stackoverflow because the leaf nodes have empty child arrays
@ibiza Yes, just bind multiple arguments, e.g. aFunction.bind(this, argument1, argument2)
|
0

While the above answer is correct. Here is another example how to pass both event and parameter to onClick function.

Your event firing element e.g a button:

<button onClick={this.editTask.bind(this, item._id)}>
  Edit item
</button>

Event handler function:

editItem(id, event) {
  event.preventDefault() // Prevents default form submission
  console.log('id: ', id) // Prints the item id 
  .
  .
  .
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.