简体   繁体   English

在ReactJS onclick中传递参数

[英]Passing parameter in ReactJS onclick

I have this React component that has items generated from a list (with a map function). 我有这个React组件,它包含从列表生成的项目(带有map函数)。 Each of those elements has a button. 每个元素都有一个按钮。 I want this button's onclick button to pass in a parameter to identify which list item's button was clicked. 我希望此按钮的onclick按钮传入一个参数,以识别单击了哪个列表项的按钮。

It looks something like this. 它看起来像这样。

var Component = React.createClass({
    assignItem: function(item){
        this.setState({item:item})
    },
    render: function(){
        var listItems = list.map(function(item){
        return <div>{item}
        <button onClick={this.assignItem(item)>Click</button>
        </div>
        })
        return <div>{listItems}</div>
    }
})

Of course that doesn't work. 当然这不起作用。 The error message says that this.assignItem is not a function. 错误消息表明this.assignItem不是函数。 I know the official React documentation suggests this: 我知道官方的React文档建议:

var handleClick = function(i, props) {
  console.log('You clicked: ' + props.items[i]);
}

function GroceryList(props) {  
  return (
    <div>
  {props.items.map(function(item, i) {
    return (
      <div onClick={handleClick.bind(this, i, props)} key={i}>{item}</div>
    );
  })}
</div>
  );
}
ReactDOM.render(
   <GroceryList items={['Apple', 'Banana', 'Cranberry']} />, mountNode
);

but that works with a function outside of the component. 但它适用于组件外部的功能。 Since I want my function to manipulate the sate, I want to keep it within the React component. 因为我希望我的函数操作状态,所以我想将它保留在React组件中。

How do I do this? 我该怎么做呢?

Original accepted answer: 原来接受的答案:

You can bind to a function on this just like the React example, however since you are rendering in a map callback you need to either pass in thisArg or use a fat arrow function: 您可以像React示例一样bind this函数,但是由于您要在map回调中渲染,因此您需要传入thisArg或使用胖箭头函数:

var Component = React.createClass({
    assignItem: function(item){
        this.setState({item:item})
    },
    render: function(){
        // bind to this.assignItem
        var listItems = list.map(function(item){
            return <div>{item}
                <button onClick={this.assignItem.bind(this, item)}>Click</button>
            </div>
        }, this); // pass in this, or use fat arrow map callback
        return <div>{listItems}</div>
    }
})

Update 2017: 2017年更新:

This is an old question using an old React API and an accordingly old answer. 这是一个使用旧的React API和相应的旧答案的老问题。 Today you should be using the class or functional React component API . 今天你应该使用类或功能React组件API For passing arguments to click handlers you can just write an inline fat arrow function and call through with whatever params you want. 要将参数传递给click处理程序,您只需编写一个内联胖箭头函数,并使用您想要的任何参数调用。 The above example ends up like this: 以上示例最终结果如下:

class MyComponent extends React.Component { // or React.PureComponent
    assignItem = item => { // bound arrow function handler
        this.setState({ item: item });
    }
    render() {
        var listItems = list.map(item => {
            // onClick is an arrow function that calls this.assignItem
            return <div>{item}
                <button onClick={e => this.assignItem(item)}>Click</button>
            </div>
        });
        return <div>{ listItems }</div>
    }
}

Note: The assignItem handler must be bound, which is done here using an arrow function as a class property . 注意:必须绑定assignItem处理程序,这是使用箭头函数作为类属性完成的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM