1

问题

我最近刚刚发现了这个库,并正在为一个反应本机应用程序实现它,但我无法理解效果协调器如何将错误传递给回滚。我正在触发以下操作,无论响应是成功还是失败,效果协调器都会直接将所有内容传递给提交。谁能告诉我我在这里做错了什么?

const saveProfileRequest = (query, link): Action => ({
  type: SAVE_PROFILE_PICTURE_REQUEST,
  meta: {
    offline: {
      effect: {
        url: BASE_URL,
        method: 'POST',
        body: JSON.stringify({
          query,
          variables: { url: link }
        })
      },
      commit: { type: SAVE_PROFILE_PICTURE_SUCCESS, meta: { link } },
      rollback: { type: SAVE_PROFILE_PICTURE_FAILURE }
    }
  }
});

期望

由于我目前正在为现有应用程序实现离线功能,因此我期望效果协调器将成功响应传递给提交并将错误响应传递给回滚。

存储配置

    // @flow
    import { createStore, applyMiddleware, combineReducers } from 'redux';
    import { composeWithDevTools } from 'redux-devtools-extension';
    import { offline } from '@redux-offline/redux-offline';
    import offlineConfig from '@redux-offline/redux-offline/lib/defaults';
    import { persistStore, persistReducer } from 'redux-persist';
    import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2';
    import thunkMiddleware from 'redux-thunk';
    import AsyncStorage from '@react-native-community/async-storage';
    import type { Store } from '@types/Store';
    import rootReducer from '@reducers/index';
    
    const persistConfig = {
      key: 'root',
      storage: AsyncStorage,
      whitelist: ['reducerOne', 'reducerTwo', 'reducerThree'],
      stateReconciler: autoMergeLevel2
    };
    
    const persistedReducer = persistReducer(
      persistConfig,
      combineReducers(rootReducer)
    );
    
    const store: Store = createStore(
      persistedReducer,
      composeWithDevTools(
        applyMiddleware(thunkMiddleware),
        offline({
          ...offlineConfig,
          retry(_action, retries) {
            return (retries + 1) * 1000;
          },
          returnPromises: true
        })
      )
    );
4

1 回答 1

2

我遇到了同样的问题,因为我们的 GraphQL API 总是返回 200,但是有一个错误对象,所以我需要自定义它。

基本上你需要自定义discard函数,在出错的情况下返回false

如果出现网络问题,默认丢弃已经返回false,但如果您的响应中有错误,并且它返回 200,您可以自定义。

丢弃文档:https ://github.com/redux-offline/redux-offline/blob/develop/docs/api/config.md#discard

默认实现(您可以将其用作初稿):https ://github.com/redux-offline/redux-offline/blob/develop/src/defaults/discard.js

于 2021-06-24T03:50:04.330 回答