Redux-offline:从未调用过提交操作

时间:2019-01-31 15:44:02

标签: react-native redux react-redux redux-offline

我正在尝试在我的react-native应用程序中实现Redux-offline,因为我已经安装了模块,并且已离线添加到我的createStore方法中:

 const store = createStore(
  myReducer,
  compose(
    offline(offlineConfig),
    applyMiddleware(thunk, promiseMiddleware())
  )
);

这是使用redux-offline的操作:

export const addResources = resource => ({
  type: "ADD_RESOURCES",
  payload: resource,
  meta: {
    offline: {
      // the network action to execute:
      effect: {
        url: "https://reqres.in/api/users",
        method: "POST",
        json: {
          body: { name: "paul rudd", movies: ["Fake data", "Fake text"] }
        }
      },
      // action to dispatch when effect succeeds:
      commit: { type: "FOLLOW_ADD_RESOURCE", meta: { resource } },
      // action to dispatch if network action fails permanently:
      rollback: { type: "ADD_RESOURCE_ROLLBACK", meta: { resource } }
    }
  }
});

为便于说明,我使用的是示例虚拟API,该API接受创建新用户并返回ID作为响应。 我的问题是,分派我的commit操作后,ADD_RESOURCES操作就永远不会被调用,另一方面,如果我发送了错误的请求,rollback将被调用。

这是我的减速机:

let tempList = state.list.concat();
  switch (action.type) {
    case ADD_RESOURCES:
      console.log("add resources action");
      return Object.assign({}, state, {
        list: tempList
      });
    case FOLLOW_ADD_RESOURCE:
      console.log("  *** FOLLOW_ADD_RESOURCE **", res);
      return Object.assign({}, state, {
        list: tempList
      });
    case ADD_RESOURCE_ROLLBACK:
      console.log("ADD_RESOURCE_ROLLBACK");
      return Object.assign({}, state, {
        list: tempList
      });
    default:
      return state;
  }

PS:我'上的像素2 XL API测试该仿真器27具有和不具有WiFi和3G网络连接

正如我所说的承诺行为永远不会出动,没有人知道我是怎么拿错?

1 个答案:

答案 0 :(得分:0)

我认为您需要对操作文件进行一些更改

export const addResources = (name,movies) => ({
  type: "ADD_RESOURCES",
  payload: name,movies
  meta: {
    offline: {
      // the network action to execute:
      effect: {
        url: "https://reqres.in/api/users/",
        method: "POST",
        body : {name: "paul rudd", movies: ["Fake data", "Fake text"]},
        headers: { 'content-type': 'application/x-www-form-urlencoded' }

      },
      // action to dispatch when effect succeeds:
      commit: { type: "FOLLOW_ADD_RESOURCE", meta: { name,movies} },
      // action to dispatch if network action fails permanently:
      rollback: { type: "ADD_RESOURCE_ROLLBACK", meta: { name,movies} }
    }
  }
});