我有在单击按钮时起作用的功能。在我添加 setInterval 方法之前,该函数运行良好,添加之后,它显示错误:_this2.setState is not a function
这是功能:
getWifiNetworksOnPress() {
setInterval(function timer() {
console.log("hello world");
wifi.loadWifiList(
wifiStringList => {
console.log(wifiStringList);
var wifiArray = JSON.parse(wifiStringList);
this.setState({
wifiList: wifiArray
});
},
error => {
console.log(error);
}
);
}, 3000);
}
这是按钮:
<Button onPress={this.getWifiNetworksOnPress.bind(this)}>
get location
</Button>
我为这个问题找到的所有答案都是将 .bind(this) 添加到 onPress,但它没有用。知道如何解决这个问题吗?