Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support latest version of NetInfo module #193

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 15 additions & 8 deletions CachedImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ const flattenStyle = ReactNative.StyleSheet.flatten;

const ImageCacheManager = require('./ImageCacheManager');

const NetInfo = require('@react-native-community/netinfo');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update package.json for the same pls


const {
View,
ImageBackground,
ActivityIndicator,
NetInfo,
Platform,
StyleSheet,
} = ReactNative;
Expand Down Expand Up @@ -63,6 +64,7 @@ class CachedImage extends React.Component {
constructor(props) {
super(props);
this._isMounted = false;
this.unsubscribable = null;
this.state = {
isCacheable: true,
cachedImagePath: null,
Expand All @@ -79,12 +81,12 @@ class CachedImage extends React.Component {

componentWillMount() {
this._isMounted = true;
NetInfo.isConnected.addEventListener('connectionChange', this.handleConnectivityChange);
this.unsubscribable = NetInfo.addEventListener(this.handleConnectivityChange);
// initial
NetInfo.isConnected.fetch()
.then(isConnected => {
NetInfo.fetch()
.then(state => {
this.safeSetState({
networkAvailable: isConnected
networkAvailable: state.isConnected
});
});

Expand All @@ -93,7 +95,12 @@ class CachedImage extends React.Component {

componentWillUnmount() {
this._isMounted = false;
NetInfo.isConnected.removeEventListener('connectionChange', this.handleConnectivityChange);

if (typeof this.unsubscribable === "function") {
this.unsubscribable();
}

// NetInfo.removeEventListener(this.handleConnectivityChange);
}

componentWillReceiveProps(nextProps) {
Expand Down Expand Up @@ -131,9 +138,9 @@ class CachedImage extends React.Component {
return this.setState(newState);
}

handleConnectivityChange(isConnected) {
handleConnectivityChange(state) {
this.safeSetState({
networkAvailable: isConnected
networkAvailable: state.isConnected
});
}

Expand Down