简体   繁体   English

如何更新 addListener React 类组件中的状态

[英]How to update state inside an addListener React Class Component

Greetings to everyone,向大家问好,

I have really no experience in working with class components as I have been using the normal functional easy ones but recently started a project that requires the class component,我真的没有使用类组件的经验,因为我一直在使用普通的功能简单的组件,但最近开始了一个需要类组件的项目,

I have the following state variable -我有以下状态变量 -

  this.state = {
      current: "City X"
    };

Now in the methods for this class, I have a function -现在在这个类的方法中,我有一个函数 -

 codeAddress(geocoder) {
        var marker;
        var data = this.state.data;
    
     marker.addListener("click", () => {
    
             
              // On click - The state value of 'current' should update to another value.

this.setState({current: 'City Y'});

                
    
                  setTimeout(() => {
                    var pointA = new google.maps.LatLng(54.976713, -1.60728);
    
                    
    
                    var request = {
                      origin: pointA,
                      destination: location,
                      travelMode: 'DRIVING'
                    };
                    directionsService.route(request, function (result, status) {
                      if (status == 'OK') {
                        directionsRenderer.setDirections(result);
                      }
                    });
    
    
                  }, 2000)
                });
    }

However this code gives an error that TypeError: Cannot read properties of undefined (reading 'setState')但是,此代码给出了一个错误,即TypeError: Cannot read properties of undefined (reading 'setState')

I have binded all the functions in the constructor and tried a bunch of different techniques of updating the state value but haven't been able to figure it out exactly just yet.我已经绑定了构造函数中的所有函数,并尝试了一系列不同的更新状态值的技术,但还不能完全弄清楚。

For what it looks like it seems like you have a problem with the scope of this , Did you bind the codeAddress Function inside the constructor?看起来你似乎对this的范围有问题,你是否在构造函数中绑定了codeAddress函数?

constructor( props ){
    super( props );
    this.codeAddress = this.codeAddress.bind(this);
  }

So what is happening is that JavaScript is trying to access the this but inside a normal Function (not an arrow function) and the this is just an object with no properties in your case.所以发生的事情是 JavaScript 试图访问 this 但在一个普通的函数(不是箭头函数)内,而 this 在你的情况下只是一个没有属性的对象。 With the bind Function we are binding the this we have in our class to that function so when you call this It will get you the this Of the class itself and will let you access all of it's properties使用bind函数,我们将类中的this绑定到该函数,因此当您调用this时,它将为您获取类本身的this并让您访问它的所有属性

Follow up on my previous answer One solution you can do is to add a binding function and call it inside the callback跟进我之前的回答您可以做的一个解决方案是添加一个绑定函数并在回调中调用它

const setCurrentOnMarkerClick = (function() { 
        this.setState({current: 'City Y'});

            

              setTimeout(() => {
                var pointA = new google.maps.LatLng(54.976713, -1.60728);

                

                var request = {
                  origin: pointA,
                  destination: location,
                  travelMode: 'DRIVING'
                };
                directionsService.route(request, function (result, status) {
                  if (status == 'OK') {
                    directionsRenderer.setDirections(result);
                  }
                });


              }, 2000)
            });          
    }).bind(this);

 marker.addListener("click", setCurrentOnMarkerClick)

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

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