简体   繁体   English

如何通过onClick事件处理函数传递带有参数的函数,并且该函数仍将函数绑定到组件的“ this”上下文?

[英]How to pass an onClick event handler a function that takes a parameter and still bind the function to the component's “this” context?

I have a class based React component called "AllProducts.jsx" that I defined like this "class AllProducts extends Component" so when I pass functions to onClick event handlers, I need to bind the context of "this", but when I try to pass one of these functions a parameter, I get an error in the console that says "cannot read property bind of undefined." 我有一个基于类的React组件,名为“ AllProducts.jsx”,我将其定义为“ AllProducts扩展组件”,因此,当我将函数传递给onClick事件处理程序时,我需要绑定“ this”的上下文,但是当我尝试将这些函数之一传递给参数,我在控制台中收到一条错误消息,提示“无法读取未定义的属性绑定”。

This is what the code looks like when it throws an error, please look at the i tag's onClick handler: 这是代码抛出错误时的样子,请查看i标记的onClick处理程序:

renderProductInfo() {

    return this.props.allProducts.map((product) => {
        return [
            <li id="shownName">{product.name}</li>,
            <li id="shownSKU">{product._id}</li>,
            <li id="shownCategory">{product.category}</li>,
            <li id="shownPrice">${product.price}</li>,
            <li id="shownQuantity">{product.quantity}</li>,
            <i onClick={this.handleEditProduct(product._id).bind(this)} 
               id="shownEdit" className="fi-pencil"></i>,
            <br />
        ];
    });

}

I worked around this issue by defining the function right inside the onClick handler like this: 我通过像这样在onClick处理程序内定义函数来解决此问题:

<i onClick={() => {
                    this.props.fetchSingleProduct(product._id).then(() => {
                        var opposite = !this.state.editProduct;

                        this.setState({
                            editProduct: opposite
                        });
                    });
                }} 
                   id="shownEdit" className="fi-pencil"></i>

Is there a way for me to just directly pass in the parameter and avoid the error? 有没有办法让我直接传递参数并避免错误?

Here's how you normally pass parameters using an arrow function: 通常,这是使用箭头函数传递参数的方式:

<i onClick={() => this.handleEditProduct(product._id)} id="shownEdit" className="fi-pencil"></i>

Also, this is how you would do it with .bind : 另外,这就是使用.bind

<i onClick={this.handleEditProduct.bind(this, product._id)} id="shownEdit" className="fi-pencil"></i>

You need to call .bind on the function, not the return value of the function call. 您需要在函数上调用.bind ,而不是函数调用的返回值。 So any parameters would go after the context (first param). 因此,任何参数都将放在上下文之后(第一个参数)。

This is calling the function, which leads to an error: 这正在调用该函数,从而导致错误:

onClick={this.handleEditProduct(product._id).bind(this)}

This is binding the function with a parameter, which is what you want: 这是将函数与参数绑定在一起,这是您想要的:

onClick={this.handleEditProduct.bind(this, product._id)}

Note that the function signature for handleEditProduct should be (productId, event) . 请注意, handleEditProduct的功能签名应为(productId, event)

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

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