Javascript 事件侦听器在普通旧函数()中不工作{}

Javascript 事件侦听器在普通旧函数()中不工作{},javascript,react-native,Javascript,React Native,我创建了如下所示的应用程序实例 import { NetInfo } from 'react-native' function App() { this.flag = { isConnected: false } ... return () => { NetInfo.addEventListener( 'connectionChange', (__connectionInfo) => { if (__connect

我创建了如下所示的应用程序实例

import { NetInfo } from 'react-native'
function App() {
this.flag = {
  isConnected: false
}
...
return () => {
       NetInfo.addEventListener(
       'connectionChange',
         (__connectionInfo) => {
          if (__connectionInfo is not empty) {
          // check if network connection type is available
         // or not if not found than re-update flag to false.
         if (__connectionInfo.type !== 'none' && __connectionInfo.effectiveType !== 'unknown') {
           // update flag.
           this.flag.isConnected = true // <-- should be updated to App(this). if you try App.flag.isConnected it will return false
         } else {
           // update false network.
           this.flag.isConnected = false
         }
       }
     }) // network event listener.
   }
}

// calling instance.
let _morphine = new App()
_morphine = _morphine() <--- I am just importing this already created instance in every component for using it as system core handler for handling every task like saving data, updating notify network change using instance.
从'react native'导入{NetInfo}
函数App(){
this.flag={
断开连接:错误
}
...
return()=>{
NetInfo.addEventListener(
“连接更改”,
(_connectionInfo)=>{
如果(_connectionInfo不为空){
//检查网络连接类型是否可用
//如果未找到,则不重新更新标志为false。
if(\uu connectionInfo.type!='none'&&&\uu connectionInfo.effectiveType!='unknown'){
//更新标志。
this.flag.isConnected=true/根据,事件侦听器应该如下所示

NetInfo.addEventListener('change',this.\u handleConnectionInfoChange);

根据,您的事件侦听器应该如下所示


NetInfo.addEventListener('change',this.\u handleConnectionInfoChange);
您在正常函数中使用的
this
(您的
应用程序
组件返回正常函数,而不是箭头函数)。该
this
将根据调用它的上下文进行计算。例如,像在代码段中那样在全局空间中调用它将使
this
成为
global
,而
this.isConnected
将是未定义的。有两个选项可以修复此问题:

  • 返回
    App
    中的箭头函数。箭头函数将
    绑定到它声明的范围,因此您将有一个常量

  • 不要使用
    this
    ,只需使用普通闭包,将
    isConnected
    声明为函数变量,您返回的函数就可以访问它

  • 关于单例设计的问题,我个人喜欢制作一个内部状态为
    isConnected
    的组件。事件处理程序可以放置在
    componentDidMount
    componentWillUnmount
    中。然后可以使用render props模式访问连接状态

    class NetInfo extends Component {
          state = {isConnected: false}
    
          componentDidMount() {
            // set up event listener
    }
    
    componentWillUnmount() {
            // remove event listener
    }
    
    render() {
    return this.props.children(this.state.isConnected)
    }
    
    }
    

    您在正常函数中使用了
    (您的
    应用程序
    组件返回正常函数,而不是箭头函数)。该
    this
    将根据调用它的上下文进行计算。例如,像在代码段中那样在全局空间中调用它将使
    this
    成为
    global
    ,而
    this.isConnected
    将是未定义的。有两个选项可以修复此问题:

  • 返回
    App
    中的箭头函数。箭头函数将
    绑定到它声明的范围,因此您将有一个常量

  • 不要使用
    this
    ,只需使用普通闭包,将
    isConnected
    声明为函数变量,您返回的函数就可以访问它

  • 关于单例设计的问题,我个人喜欢制作一个内部状态为
    isConnected
    的组件。事件处理程序可以放置在
    componentDidMount
    componentWillUnmount
    中。然后可以使用render props模式访问连接状态

    class NetInfo extends Component {
          state = {isConnected: false}
    
          componentDidMount() {
            // set up event listener
    }
    
    componentWillUnmount() {
            // remove event listener
    }
    
    render() {
    return this.props.children(this.state.isConnected)
    }
    
    }
    

    你的
    App
    函数返回一个函数。你为什么说“实例”并用
    new
    调用它?让App子对象对App可用。这样,我就可以App.Hello()和第二个返回异步。在它之后,我可以使用async。你的
    App
    函数返回一个函数。你为什么说“实例”并使用
    new
    ?调用它,使App子对象可用于App第二个问题是返回异步。在返回异步之后,我可以使用异步等待。非常感谢您的回答。对于错误,我很抱歉它是箭头函数。我的应用程序组件正在返回该函数。如果我这样做。isConnected=true就在上面的eventListner之前,它将无法工作。非常感谢您的回答。对于错误,我很抱歉它是箭头函数。我的应用程序组件正在返回哪个。如果我在上面为before eventListner执行此操作,则它将正常工作。