小编Rob*_*gan的帖子

如何在react-native中使用Redux来调度NetInfo连接更改操作?

我希望能够在我的反应本机应用程序中“持续”监视设备连接。

我已经成功使用 redux-thunk 和 redux 了。

其实我可以做到这一点

import { NetInfo } from 'react-native';

import {
    CONNECTION_CHECKING,
    CONNECTION_AVAILABLE,
    CONNECTION_UNAVAILABLE, 
} from './types';


export const connectionChecking = () => {
    return (dispatch) => {

        dispatch ({
            type: CONNECTION_CHECKING
        });

        NetInfo.getConnectionInfo()
            .then((connectionInfo) => {

                console.log('Connection info: ', connectionInfo);
                switch (connectionInfo.type) {
                    case 'cellular':
                        dispatch ({
                            type: CONNECTION_AVAILABLE,
                            payload: connectionInfo.type
                        });
                        break;
                    case 'wifi':
                        dispatch ({
                            type: CONNECTION_AVAILABLE,
                            payload: connectionInfo.type
                        });
                        break;
                    default:
                        dispatch ({
                            type: CONNECTION_UNAVAILABLE,
                        });
                }
            })
            .catch((error) => console.warn(error));

    }; …
Run Code Online (Sandbox Code Playgroud)

javascript react-native redux redux-thunk

2
推荐指数
1
解决办法
1760
查看次数

标签 统计

javascript ×1

react-native ×1

redux ×1

redux-thunk ×1