[英]TypeError _this2.getWifiList().bind is not a function. React Native componentDidMount
I'm trying to get my app to refresh a select components list of options.我正在尝试让我的应用程序刷新 select 组件选项列表。 The list will show a selection of wifi hotspots and I want the app to scan for them every 5 seconds, so I followed this guide: https://blog.stvmlbrn.com/2019/02/20/automatically-refreshing-data-in-react.html
该列表将显示一系列 wifi 热点,我希望应用程序每 5 秒扫描一次,所以我按照以下指南操作: https://blog.stvmlbrn.com/2019/02/20/automatically-refreshing-data- in-react.html
But when I run the app, I get this error:但是当我运行应用程序时,我收到了这个错误:
Error错误
Possible Unhandled Promise Rejection (id: 0):
TypeError: _this2.getWifiList().bind is not a function. (In '_this2.getWifiList().bind((0, _assertThisInitialized2.default)(_this2))', '_this2.getWifiList().bind' is undefined)
http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:187369:75
tryCallOne@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:27870:16
http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:27971:27
_callTimer@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:31410:17
_callImmediatesPass@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:31449:17
callImmediates@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:31666:33
__callImmediates@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:3610:35
http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:3396:34
__guard@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:3593:15
flushedQueue@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:3395:21
flushedQueue@[native code]
invokeCallbackAndReturnFlushedQueue@[native code]
This error comes up 10 times in a second.此错误在一秒钟内出现 10 次。 I tried removing the bind but I get the same error.
我尝试删除绑定,但我得到了同样的错误。
MainScreen.js MainScreen.js
import React, {useState} from 'react';
import { StyleSheet, PermissionsAndroid, Linking, View, TouchableOpacity } from 'react-native';
import { IndexPath, Layout, Text, Button, Select, SelectItem } from '@ui-kitten/components';
import { Icon } from 'react-native-eva-icons';
import {styles} from '../styles'
import WifiManager from "react-native-wifi-reborn";
class LocationSelector extends React.Component {
constructor(props) {
super(props);
this.state = {
selectedIndex : (new IndexPath(0)),
wifiList: null,
intervalID: null,
}
}
componentDidMount() {
this.getWifiList();
}
componentWillUnmount() {
clearInterval(this.intervalID);
}
getWifiList = async () => {
WifiManager.setEnabled(true);
await WifiManager.reScanAndLoadWifiList()
.then((data) => {
this.state.wifiList = data
this.intervalID = setTimeout( async () => {await this.getWifiList().bind(this)}, 5000);
});
}
renderOption = (title) => (
<SelectItem title={title}/>
)
render() {
console.log(this.state.selectedIndex);
const data = [
'Venue1',
'Venue2',
'Venue3',
];
const displayValue = data[this.state.selectedIndex.row];
return(
<Select
style={styles.select}
size="large"
placeholder='Default'
value={displayValue}
disabled={this.props.disabled}
accessoryLeft={PinIcon}
selectedIndex={this.state.selectedIndex}
onSelect={(index) => {(this.state.selectedIndex = index); this.forceUpdate()}}>
{data.map(this.renderOption)}
</Select>
);
}
}
class MainScreen extends React.Component {
constructor(props) {
super(props);
this.navigation = props.navigation
this.state = {
isCheckedIn: false,
}
}
render() {
return (
<Layout style={styles.container}>
<LocationSelector />
</Layout>
);
}
}
export default MainScreen;
The issue is in the way you are calling .bind()
:问题在于您调用
.bind()
的方式:
this.getWifiList().bind(this)
Note that .bind() is a method of the Function prototype, therefore, is available only when called on a function reference.请注意, .bind()是 Function 原型的方法,因此,仅当在 function 参考上调用时才可用。
In this case, you are calling .bind()
on this.getWifiList()
, which is the return value of getWifiList
, not the functon itself.在这种情况下,您在 this.getWifiList() 上调用
.bind()
this.getWifiList()
,这是getWifiList
的返回值,而不是函数本身。
To fix the error you are getting, you just need to call it on the function:要修复您遇到的错误,您只需在 function 上调用它:
this.getWifiList.bind(this)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.