简体   繁体   English

从函数返回的反应 onClick JSX

[英]React onClick JSX returned from function

I'm using the array map method below to iterate over an array of image URLs, and return some JSX with the img src.我正在使用下面的数组映射方法遍历图像 URL 数组,并返回一些带有 img src 的 JSX。

Ideally, I'd like to bind click to each image so that I can get this (clicked) img src into Redux later.理想情况下,我想将点击绑定到每个图像,以便稍后我可以将这个(点击的)img src 放入 Redux。

The below, gives me an error:下面,给了我一个错误:

Cannot read property 'changeImg' of undefined无法读取未定义的属性“changeImg”

<div className="gallery-thumbs">
        { galleryImages.map(function(img, index){
        return <img
                key={ img } 
                className="thumb-product-img" src={ img }
                onClick={ this.changeImg.bind(this) } />;
      }) }
    </div>    

However, when I move the onClick up a level it doesn't give me the error... the only problem is, the context of this is lost.但是,当我将onClick向上移动一个级别时,它不会给我错误……唯一的问题是,上下文丢失了。

<div className="gallery-thumbs" onClick={ this.changeImg.bind(this) }>
        { galleryImages.map(function(img, index){
        return <img
                key={ img }
                className="thumb-product-img" src={ img } />;
      }) }
    </div>

tl:dr; tl:博士; how can I bind click to JSX returned outside of my render method?如何将单击绑定到渲染方法之外返回的 JSX?

It will probably work if you change:如果您更改,它可能会起作用:

 <div className="gallery-thumbs">
        { galleryImages.map((img, index) => { //arrow function
        return <img
                key={ img } 
                className="thumb-product-img" src={ img }
                onClick={ this.changeImg.bind(this) } />;
      }) }
    </div>  

I assume you loose the context while iterating over the array with map .我假设您在使用map迭代数组时失去了上下文。 A simple change from function to arrow function will probably do the trick.functionarrow函数的简单更改可能会奏效。

Here you can read more about arrow functions and their use as event handlers and more. 在这里您可以阅读更多关于arrow函数及其作为事件处理程序的用途等。

It means the lexical context this is undefined .这意味着词法上下文thisundefined It's the case when you use it inside a function call in strict mode ( which is the default mode for es modules).当您在严格模式(这是 es 模块的默认模式)的函数调用中使用它时就是这种情况。

what you could do is, inside the render method of your component class :你可以做的是,在你的组件类的render方法中:

<div className="gallery-thumbs">
    { galleryImages.map((img, index) => {
    return <img
            key={ img } 
            className="thumb-product-img" src={ img }
            onClick={ e => this.changeImg(e) } />;
  }) }
</div>    

The arrow function syntax creates a new function which does not have a lexical context, which means that invoking this in its body will reference the parent lexical context, in your case your component.箭头函数语法创建了一个没有词法上下文的新函数,这意味着在其主体中调用this将引用父词法上下文,在您的情况下是您的组件。

Because what you want is clearly identified : the src attribute of the image, you could, as an optimization, not create a new handler but instead create a generic one.因为您想要的内容已明确标识:图像的 src 属性,作为优化,您可以不创建新的处理程序,而是创建一个通用的处理程序。

const handle = e => this.changeImg(e.currentTarget.src);
<div className="gallery-thumbs">
    { galleryImages.map((img, index) => {
    return <img
            key={ img } 
            className="thumb-product-img" src={ img }
            onClick={ handle } />;
  }) }
</div>    

you can use arrow function as @Nocebo said, you can also use map function like this :你可以像@Nocebo 所说的那样使用arrow function ,你也可以像这样使用 map 函数:

<div className="gallery-thumbs">
    { 
      galleryImages.map(function(img, index){
       return <img
            key={ img } 
            className="thumb-product-img" src={ img }
            onClick={ this.changeImg.bind(this) } />;
      }, this) 
    }
</div> 

map function take a second argument which will be the Value to use as this when executing callback map函数采用第二个参数,这将是执行callback时用作 this 的Value

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

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