我正在尝试在我的 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,它接受新用户的创建并返回一个 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:我在 Pixel 2 xl API 27 模拟器上测试这个,有和没有 wifi 和 3G 互联网连接。
正如我所说,提交操作永远不会被派发,有谁知道我做错了什么?