0

Well, I'm trying to remove the event to the listener but I got the error.

Here's my code:

import { useEffect, useState } from "react";

import NetInfo, { NetInfoState } from "@react-native-community/netinfo";

export const useInternetStatus = () => {
   const [reachable, setReachable] = useState(false);

  useEffect(() => {
      const subscribe = (state: NetInfoState) => 
  setReachable(state.isInternetReachable ?? false);

  NetInfo.addEventListener(subscribe);

       return () => NetInfo.removeEventListener(subscribe);
  },[]);

  return reachable;
};

Error:

Property 'removeEventListener' does not exist on type '{ configure: (configuration: Partial<NetInfoConfiguration>) => void; fetch: (requestedInterface?: string | undefined) => Promise<NetInfoState>; addEventListener: (listener: NetInfoChangeHandler) => NetInfoSubscription; useNetInfo: (configuration?: Partial<...> | undefined) => NetInfoState; }'.

So, looking inside NetInfo types, I noticed that removeEventListener isn't defined on type. But, my question is: It's really necessary? Do I really need to remove event listener in this case?

4

0 回答 0