Giúp lỗi code React

Mình đang học React, trong khi code một trang nhận diện gương mặt dùng clarifai thì gặp lỗi như sau, mình làm theo như Document https://www.clarifai.com/models/face-detection-image-recognition-model-a403429f2ddf4b49b307e318f00e528b-detection

Đây là đoạn code theo document của mình:

app.models.predict(Clarifai.FACE_DETECT_MODEL, this.state.input).then(
  function(response) {
    this.displayFaceBox(this.calculateFaceLocation(response));
  },
  function(err) {
    console.log(err);
  }
);

thì bị gặp lỗi nhưng khi chuyển sang viết ES6 thì lại chạy ok:

app.models
  .predict(Clarifai.FACE_DETECT_MODEL, this.state.input)
  .then(response => this.displayFaceBox(this.calculateFaceLocation(response)))
  .catch(err => console.log(err));  

Ai biết lý do giải thích cho mình với. Mình xin cảm ơn nhiều

2 Likes

Mình nghĩ là do cái this !!!

3 Likes

Trong strict mode thì this trong function nếu không được bind sẽ là undefined, nhưng arrow function thì không bị, do arrow function lấy this từ lexical context của nó :slight_smile:

5 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?