简体   繁体   English

TypeError _this2.getWifiList().bind 不是 function。 反应原生组件DidMount

[英]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.

相关问题 'TypeError: el.addEventListener 不是 function。' 在 react-native - 'TypeError: el.addEventListener is not a function.' in react-native react native TypeError: this.setState is not a function.'this.setState' is undefined) - react native TypeError: this.setState is not a function.'this.setState' is undefined) 在 componentDidMount 中调用函数反应原生 - calling a function in componentDidMount react native React Native 问题:[未处理的 promise 拒绝:TypeError:f 不是 function。 (在 'f()' 中,'f' 未定义)] - React Native issue : [Unhandled promise rejection: TypeError: f is not a function. (In 'f()', 'f' is undefined)] React Native:TypeError:navigationRef.isReady 不是函数。 (在&#39;navigationRef.isReady()&#39; - React Native: TypeError: navigationRef.isReady is not a function. (In 'navigationRef.isReady()' 可能未处理的 Promise 拒绝(id:20):TypeError:_util.base64.tI 不是 function。 反应原生 - Possible Unhandled Promise Rejection (id: 20): TypeError: _util.base64.tI is not a function. React Native firebase auth: TypeError: (0, _reactNative.getReactNativePersistence) 不是 function。react-native EXPO - firebase auth: TypeError: (0, _reactNative.getReactNativePersistence) is not a function. react-native EXPO 减速机不是 function。 反应原生 Redux - Reducer is not a function. React Native Redux TypeError:不是函数。 正确的绑定方法。 JS中的引用 - TypeError: is not a function. Right way to bind this. references in JS React - 在 componentDidMount function 中接收“未捕获的 TypeError:this.setState 不是函数” - React - Receiving "Uncaught TypeError: this.setState is not a function" in componentDidMount function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM