最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - In ReactJS how would I bind "this" to the parent component inside a map() function - Stack Overfl

programmeradmin3浏览0评论

Given the following javascript, in order to pass the updateSelectedPages method from the parent react component to the Image component inside of the .map() function, I first have to assign it to a variable and then use that variable inside the function. Is there some way to bind this inside the function to be the parent react component so I don't have to use a temporary variable?

var pageNumber = 0;
var updateSelectedPages = this.updateSelectedPages;
var images = this.props.resources.map(function(resource) {
  var arr = [];
  for(var x=0; x<resource.numPages; x++) {
    pageNumber++;
    arr.push(
      <Image src={'import/previewImage/' + resource.encodedName + '?page=' + pageNumber} number={pageNumber} updateSelectedPages={updateSelectedPages} key={pageNumber} />
    );
  }
  return arr;
}).reduce(function(a, b) {
  return a.concat(b);
});

Given the following javascript, in order to pass the updateSelectedPages method from the parent react component to the Image component inside of the .map() function, I first have to assign it to a variable and then use that variable inside the function. Is there some way to bind this inside the function to be the parent react component so I don't have to use a temporary variable?

var pageNumber = 0;
var updateSelectedPages = this.updateSelectedPages;
var images = this.props.resources.map(function(resource) {
  var arr = [];
  for(var x=0; x<resource.numPages; x++) {
    pageNumber++;
    arr.push(
      <Image src={'import/previewImage/' + resource.encodedName + '?page=' + pageNumber} number={pageNumber} updateSelectedPages={updateSelectedPages} key={pageNumber} />
    );
  }
  return arr;
}).reduce(function(a, b) {
  return a.concat(b);
});
Share Improve this question asked Nov 19, 2014 at 18:37 Patrick GrimardPatrick Grimard 7,1269 gold badges52 silver badges69 bronze badges 2
  • 1 Or you could just declare a top level variable self and set it to this and access it within the function :) – Dhruva Sagar Commented Nov 20, 2014 at 17:55
  • @DhruvaSagar i'm not sure it's gonna work 100%. Look at answers for better alternatives. – Maxim Veksler Commented Apr 25, 2016 at 13:36
Add a comment  | 

2 Answers 2

Reset to default 21

Array.prototype.map take second argument that set the context for the callback function. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/map

So the following should be enough, if I understood you question correctly :)

var pageNumber = 0;
var images = this.props.resources.map(function(resource) {
  var arr = [];
  for(var x=0; x<resource.numPages; x++) {
    pageNumber++;
    arr.push(
      <Image src={'import/previewImage/' + resource.encodedName + '?page=' + pageNumber} number={pageNumber} updateSelectedPages={this.updateSelectedPages} key={pageNumber} />
    );
  }
  return arr;
}, this).reduce(function(a, b) {
  return a.concat(b);
});

Edit 2+ years later: Nowadays you can most likely utilize arrow functions in your code (either by native support if only targeting the latest browser or via build-tools like babel), and other than being shorter to write they also have lexical scoping for this which removes the need for binding or passing the second arg to array.map()

let pageNumber = 0;
const images = this.props.resources.map(resource => { // <-- using arrow function removes the need to worry about `this`
  const arr = [];
  for(let x = 0; x < resource.numPages; x++) {
    pageNumber++;
    arr.push(
      <Image
        src={`import/previewImage/${resource.encodedName}?page=${pageNumber}`}
        number={pageNumber}
        updateSelectedPages={this.updateSelectedPages}
        key={pageNumber}
      />
    );
  }
  return arr;
}).reduce(function(a, b) {
  return a.concat(b);
});

You can bind this as follows:

var pageNumber = 0;
var images = this.props.resources.map(function(resource) {
  var arr = [];
  for(var x=0; x<resource.numPages; x++) {
    pageNumber++;
    arr.push(
      <Image src={'import/previewImage/' + resource.encodedName + '?page=' + pageNumber} number={pageNumber} updateSelectedPages={this.updateSelectedPages} key={pageNumber} />
    );
  }
  return arr;
}.bind(this)).reduce(function(a, b) {
  return a.concat(b);
});

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论