微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在配置 react-native-offline 时得到 undefined 不是对象 'state.network.isConnected 与 redux-persist 和 redux-thunk 一起使用时 当前行为预期行为如何复制环境

如何解决在配置 react-native-offline 时得到 undefined 不是对象 'state.network.isConnected 与 redux-persist 和 redux-thunk 一起使用时 当前行为预期行为如何复制环境

当前行为

-尝试使用 react-native-offline redux-thunk redux-persist 创建一个可行的解决方案 - 在运行应用程序时获取 undefined is not an object (evaluating 'state.network.isConnected')app.js


import React from "react";
import { Provider } from "react-redux";
import configureStore from "@store/configure-store.js";
import { PersistGate } from "redux-persist/es/integration/react";
import { ReduxNetworkProvider } from 'react-native-offline';

import Router from "@appComponents/router.js";

const { persistor,store } = configureStore;

export default App = props => (
  <Provider store={store}>
    <PersistGate persistor={persistor}>
      <ReduxNetworkProvider>
        <Router />
      </ReduxNetworkProvider>
    </PersistGate>
  </Provider>
);


app.js


import { createStore,applyMiddleware } from "redux";
import { persistStore,persistReducer } from "redux-persist";
import AsyncStorage from "@react-native-async-storage/async-storage";
import ReduxThunk from "redux-thunk";
import ReduxLogger from "redux-logger";
import hardSet from "redux-persist/lib/stateReconciler/hardSet";
import rootReducer from "@store/store";
import { createNetworkMiddleware } from "react-native-offline";
import { createTransform } from 'redux-persist';


import { user } from "@constants/action-types";

const { loginSuccess } = user;

const networkTransform = createTransform(
  (inboundState,key) => {
    const actionQueue = [];

    inboundState.actionQueue.forEach(action => {
      if (typeof action === 'function') {
        actionQueue.push({
          function: action.Meta.name,args: action.Meta.args,});
      } else if (typeof action === 'object') {
        actionQueue.push(action);
      }
    });

    return {
      ...inboundState,actionQueue,};
  },(outboundState,key) => {
    const actionQueue = [];

    outboundState.actionQueue.forEach(action => {
      if (action.function) {
        const actionFunction = actions[action.function];
        actionQueue.push(actionFunction(...action.args));
      } else {
        actionQueue.push(action);
      }
    });

    return { ...outboundState,actionQueue };
  },{ whitelist: ['network'] },);

const persistConfig = {
  key: 'root',storage: AsyncStorage,stateReconciler: hardSet,whitelist: ["app","user"],transforms: [networkTransform],};


const networkMiddleware = createNetworkMiddleware({
  regexActionType: /^OTHER/,actionTypes: [loginSuccess],queueReleaseThrottle: 200
});

const persistedReducer = persistReducer(persistConfig,rootReducer);
const isDevMode = Boolean(__DEV__);
const middleWares = isDevMode ? [networkMiddleware,ReduxThunk,ReduxLogger] : [networkMiddleware,ReduxThunk];
const store = createStore(persistedReducer,{},applyMiddleware(...middleWares));
const persistor = persistStore(store);
export default { store,persistor };


pakage.json

"dependencies": {
    "@react-native-async-storage/async-storage": "^1.15.5","@react-native-community/masked-view": "^0.1.11","@react-native-community/netinfo": "^5.9.10","@react-navigation/native": "^5.9.4","@react-navigation/stack": "^5.14.5","axios": "^0.21.1","react": "17.0.1","react-native": "0.64.2","react-native-gesture-handler": "^1.10.3","react-native-offline": "^5.8.0","react-native-reanimated": "^2.2.0","react-native-safe-area-context": "^3.2.0","react-native-screens": "^3.3.0","react-redux": "^7.2.4","redux": "^4.1.0","redux-persist": "^6.0.0","redux-thunk": "^2.3.0"
  }

action.js


const UserActions = {
    loginTap: tapped => {
        function thunk(dispatch,getState) {
            const user = { email: 'bsuri555@gmail.com',password: 'Rohtash*1' };
            dispatch({ type: loginStart })
            if (!tapped) {
                axios.post(userLogin,user).then(function (response) {
                    console.log("response",response.data);
                    dispatch({ type: loginSuccess,payload: tapped })


                }).catch(function (error) {
                    dispatch({ type: loginFailure,payload: error })
                })
            }
            else {
                dispatch({ type: loginTapped,payload: tapped })
            }


        }
        thunk.interceptInOffline = true;
        return thunk;
    }
};

预期行为

  • 使其与 redux-peersist 一起工作,然后将工作解决方案合并到发布版本。

如何复制

  • 配置完所有包后,link 无法启动应用

环境

软件 版本
react-native-offline "^5.8.0"
反应原生 "0.64.2"
节点 v15.9.0
npm 或 yarn npm 7.5.3

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?