使用 react-native-web 时,NetInfo 适用于 Android 和 iOS,但不适用于 web。
为了确认为 Web 运行的代码与 Android 相同,我登录控制台,componentDidMount
Android 和 Web 都显示文本。然而,这些事件似乎不适用于网络版本。
// src/App.js
import React from 'react';
import { Provider } from 'react-redux';
import store from './utilities/storage/store';
import EntryScreen from './screens/EntryScreen';
import Routing, { Router } from './utilities/routing/index';
import { NetInfo } from 'react-native';
const Route = Routing.Route;
class App extends React.Component {
componentDidMount() {
console.log('App mounted');
NetInfo.isConnected.addEventListener('connectionChange', this.handleConnectivityChange);
}
componentWillUnmount() {
NetInfo.isConnected.removeEventListener('connectionChange', this.handleConnectivityChange);
}
/**
* Listens for any internet connectivity changes.
* @param isConnected The internet connection status
*/
handleConnectivityChange = (isConnected) => {
console.log ('bam');
console.log ('Connected:', isConnected);
};
render() {
return (
<Provider store={store}>
<Router>
<Route path="/" component={EntryScreen} />
</Router>
</Provider>
);
}
}
export default App;
我希望,基于 NetInfo 适用于 web 和 Android 的文档。如果没有,请有人提供一种方法来做到这一点。
"react": "^16.2.0",
"react-art": "^16.2.0",
"react-dom": "^16.2.0",
"react-native": "0.57.8",
"react-native-web": "0.9.13",