2015-12-08 17:30:07 +00:00
|
|
|
/**
|
2016-07-12 12:51:57 +00:00
|
|
|
* Copyright (c) 2013-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
2015-12-08 17:30:07 +00:00
|
|
|
* The examples provided by Facebook are for non-commercial testing and
|
|
|
|
* evaluation purposes only.
|
|
|
|
*
|
|
|
|
* Facebook reserves all rights not expressly granted.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
|
|
|
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* @flow
|
2017-02-25 11:05:32 +00:00
|
|
|
* @providesModule NetInfoExample
|
2015-12-08 17:30:07 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-04-09 03:36:40 +00:00
|
|
|
const React = require('react');
|
|
|
|
const ReactNative = require('react-native');
|
2015-12-08 17:30:07 +00:00
|
|
|
const {
|
|
|
|
NetInfo,
|
|
|
|
Text,
|
|
|
|
View,
|
|
|
|
TouchableWithoutFeedback,
|
2016-04-09 03:36:40 +00:00
|
|
|
} = ReactNative;
|
2015-12-08 17:30:07 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
class ConnectionInfoSubscription extends React.Component {
|
|
|
|
state = {
|
|
|
|
connectionInfoHistory: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount() {
|
2015-12-08 17:30:07 +00:00
|
|
|
NetInfo.addEventListener(
|
|
|
|
'change',
|
|
|
|
this._handleConnectionInfoChange
|
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2015-12-08 17:30:07 +00:00
|
|
|
NetInfo.removeEventListener(
|
|
|
|
'change',
|
|
|
|
this._handleConnectionInfoChange
|
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_handleConnectionInfoChange = (connectionInfo) => {
|
2015-12-08 17:30:07 +00:00
|
|
|
const connectionInfoHistory = this.state.connectionInfoHistory.slice();
|
|
|
|
connectionInfoHistory.push(connectionInfo);
|
|
|
|
this.setState({
|
|
|
|
connectionInfoHistory,
|
|
|
|
});
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
|
|
|
|
2015-12-08 17:30:07 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<Text>{JSON.stringify(this.state.connectionInfoHistory)}</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-12-08 17:30:07 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
class ConnectionInfoCurrent extends React.Component {
|
|
|
|
state = {
|
|
|
|
connectionInfo: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount() {
|
2015-12-08 17:30:07 +00:00
|
|
|
NetInfo.addEventListener(
|
|
|
|
'change',
|
|
|
|
this._handleConnectionInfoChange
|
|
|
|
);
|
|
|
|
NetInfo.fetch().done(
|
|
|
|
(connectionInfo) => { this.setState({connectionInfo}); }
|
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2015-12-08 17:30:07 +00:00
|
|
|
NetInfo.removeEventListener(
|
|
|
|
'change',
|
|
|
|
this._handleConnectionInfoChange
|
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_handleConnectionInfoChange = (connectionInfo) => {
|
2015-12-08 17:30:07 +00:00
|
|
|
this.setState({
|
|
|
|
connectionInfo,
|
|
|
|
});
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
|
|
|
|
2015-12-08 17:30:07 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<Text>{this.state.connectionInfo}</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-12-08 17:30:07 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
class IsConnected extends React.Component {
|
|
|
|
state = {
|
|
|
|
isConnected: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount() {
|
2015-12-08 17:30:07 +00:00
|
|
|
NetInfo.isConnected.addEventListener(
|
|
|
|
'change',
|
|
|
|
this._handleConnectivityChange
|
|
|
|
);
|
|
|
|
NetInfo.isConnected.fetch().done(
|
|
|
|
(isConnected) => { this.setState({isConnected}); }
|
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2015-12-08 17:30:07 +00:00
|
|
|
NetInfo.isConnected.removeEventListener(
|
|
|
|
'change',
|
|
|
|
this._handleConnectivityChange
|
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_handleConnectivityChange = (isConnected) => {
|
2015-12-08 17:30:07 +00:00
|
|
|
this.setState({
|
|
|
|
isConnected,
|
|
|
|
});
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
|
|
|
|
2015-12-08 17:30:07 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<Text>{this.state.isConnected ? 'Online' : 'Offline'}</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-12-08 17:30:07 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
class IsConnectionExpensive extends React.Component {
|
|
|
|
state = {
|
|
|
|
isConnectionExpensive: (null : ?boolean),
|
|
|
|
};
|
|
|
|
|
|
|
|
_checkIfExpensive = () => {
|
2016-02-15 21:33:28 +00:00
|
|
|
NetInfo.isConnectionExpensive().then(
|
|
|
|
isConnectionExpensive => { this.setState({isConnectionExpensive}); }
|
2015-12-08 17:30:07 +00:00
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
|
|
|
|
2015-12-08 17:30:07 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<TouchableWithoutFeedback onPress={this._checkIfExpensive}>
|
|
|
|
<View>
|
|
|
|
<Text>Click to see if connection is expensive:
|
|
|
|
{this.state.isConnectionExpensive === true ? 'Expensive' :
|
|
|
|
this.state.isConnectionExpensive === false ? 'Not expensive'
|
|
|
|
: 'Unknown'}
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
</TouchableWithoutFeedback>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-12-08 17:30:07 +00:00
|
|
|
|
|
|
|
exports.title = 'NetInfo';
|
|
|
|
exports.description = 'Monitor network status';
|
|
|
|
exports.examples = [
|
|
|
|
{
|
|
|
|
title: 'NetInfo.isConnected',
|
|
|
|
description: 'Asynchronously load and observe connectivity',
|
2016-10-16 11:11:59 +00:00
|
|
|
render(): React.Element<any> { return <IsConnected />; }
|
2015-12-08 17:30:07 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'NetInfo.update',
|
|
|
|
description: 'Asynchronously load and observe connectionInfo',
|
2016-10-16 11:11:59 +00:00
|
|
|
render(): React.Element<any> { return <ConnectionInfoCurrent />; }
|
2015-12-08 17:30:07 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'NetInfo.updateHistory',
|
|
|
|
description: 'Observed updates to connectionInfo',
|
2016-10-16 11:11:59 +00:00
|
|
|
render(): React.Element<any> { return <ConnectionInfoSubscription />; }
|
2015-12-08 17:30:07 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
platform: 'android',
|
|
|
|
title: 'NetInfo.isConnectionExpensive (Android)',
|
2015-12-09 22:51:43 +00:00
|
|
|
description: 'Asynchronously check isConnectionExpensive',
|
2016-10-16 11:11:59 +00:00
|
|
|
render(): React.Element<any> { return <IsConnectionExpensive />; }
|
2015-12-08 17:30:07 +00:00
|
|
|
},
|
|
|
|
];
|