1

I'm using react-native-meteor module in react-native-app to connect it to Meteor server.
On Calling the following function in App.js ,

Meteor.connect('ws://192.168.x.x:3000/websocket'); 

I was getting this error

Invariant Violation: NetInfo has been removed from React.Native.It can now be installed and imported from 'react-native-netinfo' 

An issue is already opened here NetInfo Issue but not resolved yet.

ImFarhad
  • 2,669
  • 2
  • 18
  • 30

2 Answers2

0

NetInfo has been removed from react-native core. It should be installed as a stand alone dependency using following command:

yarn add @react-native-community/netinfo
or:
npm install --save @react-native-community/netinfo

step by step instructions can be found on this github link

I followed these steps mentioned on the above mentioned link , but it didn't resolve the issue completely. After digging the issue a little bit, I found out that in node_modules/react-native-meteor/src/Meteor.js , They are still importing NetInfo from 'react-native' and using the old functions

import { Platform, View, NetInfo } from 'react-native'; 
NetInfo.isConnected.fetch().then(
  (connected )  => {
  if(connected)
    NetInfo.isConnected.addEventListener('connectionChange', isConnected => {
    if (isConnected && Data.ddp.autoReconnect) {
      Data.ddp.connect();
    }
  });
}
);

which is obsoleted now.

It has to be imported from @react-native-community/netinfo
After doing the following changes in node_modules/react-native-meteor/src/Meteor.js . Error fixed:

import { Platform, View } from 'react-native';
import NetInfo from "@react-native-community/netinfo";

NetInfo.fetch().then(state => {
  console.log("Connection type", state.type);
  console.log("Is connected?", state.isConnected);
  let isConnected = state.isConnected;
   if(isConnected)
        NetInfo.isConnected.addEventListener('connectionChange', isConnected => {
        if (isConnected && Data.ddp.autoReconnect) {
          Data.ddp.connect();
        }
      });
});
ImFarhad
  • 2,669
  • 2
  • 18
  • 30
0

ImFarhad Does that still work for you? Using your changes to meteor.js I get the error "RNCNetInfo.getCurrentState got 3 arguments, expected 2". Do you know a way around this?

HaakonFlaar
  • 387
  • 2
  • 4
  • 15
  • yup, It's working for me. have u followed all the instructions given at this Link https://github.com/react-native-community/react-native-netinfo (mentioned in the answer above). – ImFarhad Jan 01 '20 at 16:16
  • I'm using expo and I can't find my "android/build.gradle" file. Do you happen to know how to get around that? – HaakonFlaar Jan 02 '20 at 00:22
  • @HaakonFlaar Did you have any luck figuring this out? I'm having the same issue. I'm not seeing anywhere in the source code where 3 arguments are expected. – bgmaster Jan 12 '20 at 10:47