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

fix RCTNetInfo first time connection status #20820

Closed
wants to merge 1 commit into from

Conversation

karanjthakkar
Copy link
Contributor

Fixes #20804, #8615, #18368 (comment)

Test Plan:

  1. Works without connectionChange event listener

Test Code:

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, NetInfo} from 'react-native';

export default class App extends Component {
  state = {
    isConnected: false
  };

  componentWillMount() {
    NetInfo.isConnected.fetch().then(isConnected => {
      this.setState({
        isConnected
      });
    });
  }

  render() {
    return (
      <View style={styles.container}>
        <Text>{`isConnected: ${this.state.isConnected}`}</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
});

The code shown above will now return the right connection status. If you're connected to Wifi or cellular, it will return true, else false. Previously, it always returned false for the first time.

img_1594

  1. Works with connectionChange event listener

Test Code:

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, NetInfo} from 'react-native';

export default class App extends Component {
  state = {
    isConnected: false
  };

  componentWillMount() {
    NetInfo.isConnected.fetch().then(isConnected => {
      this.setState({
        isConnected
      });
    });
    const handleFirstConnectivityChange = (isConnected) => {
      this.setState({
        isConnected
      });
    }
    NetInfo.isConnected.addEventListener(
      'connectionChange',
      handleFirstConnectivityChange
    );
  }

  render() {
    return (
      <View style={styles.container}>
        <Text>{`isConnected: ${this.state.isConnected}`}</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
});

The change done in this PR does not create any regression in the event listener flow. This still works perfectly fine.

Release Notes:

[IOS] [BUGFIX] [NETINFO] - fixes RCTNetInfo incorrect first time connection status

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 23, 2018
@react-native-bot react-native-bot added 🌐Networking Related to a networking API. ✅Release Notes labels Aug 23, 2018
@karanjthakkar
Copy link
Contributor Author

I don't understand why the tests are failing. The logs seem to be unrelated to the changes made in this PR. Maybe someone else knows what the issue is?

@karanjthakkar
Copy link
Contributor Author

cc: @hramos, can you help here? 🙂

@facebook-github-bot facebook-github-bot added the Import Started This pull request has been imported. This does not imply the PR has been approved. label Sep 12, 2018
Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

hramos is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@react-native-bot
Copy link
Collaborator

@karanjthakkar merged commit 36199d3 into facebook:master.


Once this commit is added to a release, you will see the corresponding version tag below the description at 36199d3. If the commit has a single master tag, it is not yet part of a release.

@facebook facebook locked as resolved and limited conversation to collaborators Sep 12, 2018
@react-native-bot react-native-bot added the Merged This PR has been merged. label Sep 12, 2018
gengjiawen pushed a commit to gengjiawen/react-native that referenced this pull request Sep 14, 2018
Summary:
Fixes facebook#20804, facebook#8615, facebook#18368 (comment)
Pull Request resolved: facebook#20820

Differential Revision: D9798488

Pulled By: hramos

fbshipit-source-id: bd93a857b622edfbefdbd1baea746f27658f1366
kelset pushed a commit that referenced this pull request Sep 21, 2018
Summary:
Fixes #20804, #8615, #18368 (comment)
Pull Request resolved: #20820

Differential Revision: D9798488

Pulled By: hramos

fbshipit-source-id: bd93a857b622edfbefdbd1baea746f27658f1366
@hramos hramos removed Import Started This pull request has been imported. This does not imply the PR has been approved. labels Feb 6, 2019
matt-oakes pushed a commit to matt-oakes/react-native that referenced this pull request Feb 7, 2019
Summary:
Fixes facebook#20804, facebook#8615, facebook#18368 (comment)
Pull Request resolved: facebook#20820

Differential Revision: D9798488

Pulled By: hramos

fbshipit-source-id: bd93a857b622edfbefdbd1baea746f27658f1366
t-nanava pushed a commit to microsoft/react-native-macos that referenced this pull request Jun 17, 2019
Summary:
Fixes facebook#20804, facebook#8615, facebook#18368 (comment)
Pull Request resolved: facebook#20820

Differential Revision: D9798488

Pulled By: hramos

fbshipit-source-id: bd93a857b622edfbefdbd1baea746f27658f1366
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. 🌐Networking Related to a networking API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

iOS: NetInfo.isConnected returns always false (still)
4 participants