【问题标题】:react-native globally prompt user to enable wifi or cellular when network failedreact-native 全局提示用户在网络失败时启用 wifi 或蜂窝
【发布时间】:2018-07-24 05:21:45
【问题描述】:

我想知道当全球 axios 网络出现故障时如何提示用户启用 wifi 或蜂窝。 我使用 axios 拦截器来捕获网络错误,我使用 NetInfo 来检测没有互联网连接,但我不知道如何提示用户启用 wifi 或蜂窝?

axios.interceptors.response.use(null, function(error) {
  if (!error.status) {
    // network error

    NetInfo.isConnected.fetch().then(isConnected => {
      console.log(isConnected ? 'online' : 'offline');

      if(!isConnected)
      {
        // how to prompt user enable wifi or cellular 
        
        
      }

    });

  }

  return Promise.reject(error);
});

【问题讨论】:

    标签: react-native axios interceptor


    【解决方案1】:

    我认为有另一种方法可以通知用户设备的连接丢失,无论屏幕如何。 我的方法是监听组件中的网络状态变化,并将这个状态更新到reducer。如果你使用 react-navigation,你可以监听一个与 redux 连接的组件(eg.ReduxNavigation)。这样,每个屏幕都可以显示丢失的网络状态,并通知用户检查网络连接。

    NeworkStatus.js

    class NetworkStatus extends Component {
     componentWillMount() {
      //For device's network status
      NetInfo.isConnected.fetch().done(data => {
        this.props.updateData({
          prop: "networkStatus",
          value: data
        });
      }); 
     }
    
     componentDidMount() {
      //Listener for device's network status
      NetInfo.isConnected.addEventListener(
        "connectionChange",
        this.handleConnectivityChange
     );
     }
    
     handleConnectivityChange = change => {
      this.props.updateData({
        prop: "networkStatus",
        value: change
      });
     };
    
     render() {
        return null;
     }
    }
    
    const mapStateToProps = state => {
      return state;
    };
    
    export default connect(mapStateToProps,{updateData})(NetworkStatus);
    

    ReduxNavigation.js

    class ReduxNavigation extends React.Component {
     render() {
      return (
      <View style={{ flex: 1 }}>
        <NetworkStatus />
        <AppNavigation/>
      </View>
      );
     }
    }
    
    const mapStateToProps = state => ({ return state});
    export default connect(mapStateToProps)(ReduxNavigation);
    

    Screen1.js

    class Screen1 extends Component {
       render(){
           return (
             <View>
                <Text>Home</Text>
                {this.props.networkStatus ? null : (
                 <View>
                   <Text>No Internet Connection</Text>
                 </View>
                 )}
             </View>
           );
       }
    }
    
    const mapStateToProps = state => {
      const { networkStatus } = state.common; // common is reducer name
      return {networkStatus};
    };
    export default connect(mapStateToProps)(Screen1);
    

    这是我在我的应用程序中所做的方式。如果设备已连接,则不显示任何内容。但是,如果它没有连接,用户可以看到网络信息。您可以决定如何为您的屏幕显示和 UI 设计。我希望它会帮助你。

    【讨论】:

    • 谢谢兄弟,但我的主要问题是提示用户启用移动数据或 wifi,我想在全球范围内这样做,所以我不需要将其代码添加到我的所有组件中
    • 欢迎,兄弟。对不起,我的回答帮不了你。我只是觉得调用网络请求时在 axios 拦截器中提示用户对我来说很奇怪。所以,我可以只在我想要的某些屏幕上显示这个网络状态。希望你能找到答案。
    猜你喜欢
    • 1970-01-01
    • 2017-11-23
    • 1970-01-01
    • 2018-02-15
    • 2018-09-05
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    相关资源
    最近更新 更多