首页 > 解决方案 > 为什么带有 react-native 的 redux-offline 不发送 POST 请求?

问题描述

我有delete_task操作,将任务 id 发布到 PHP 文件以删除任务,虽然我正确触发了操作并且我在控制台中看到了效果但任务没有被删除,我尝试了很多次使用 GET 或 POST 或只是把id 在 URL 中静态但没有任何变化,谁能帮我解决问题

任务Action.js

export  const delete_task = id => ({
    type: DELETE_TASK,
    payload: {id},
    meta: {
        offline: {
            // the network action to execute:
            effect: { url: 'http://localhost/todos/remove.php', method: 'POST',body: {id} },
            // action to dispatch when effect succeeds:
            commit: { type: DELETE_SUCCESS, meta: { id } },
            // action to dispatch if network action fails permanently:
            rollback: { type: DELETE_FAILED, meta: { id } }
        }
    }
});

store.js

import React, { Component } from 'react';
import { applyMiddleware, createStore, compose } from 'redux';
import { offline } from '@redux-offline/redux-offline';
import offlineConfig from '@redux-offline/redux-offline/lib/defaults';
import {Provider} from 'react-redux';
import logger from 'redux-logger';

import RootNavigator from "./config/routes";
import reducers from './reducers'


// store
const middleware = [];
if(process.env.NODE_ENV === 'development'){
    middleware.push(logger);
}
const store = createStore(
    reducers,
    undefined,
    compose(
        applyMiddleware(...middleware),
        offline(offlineConfig)
    )
);

export default class App extends Component {

    render() {
        return (
            <Provider store={store}>
            <RootNavigator />
            </Provider>

        )
    }
}

记录器结果

标签: react-nativereduxreact-reduxredux-offline

解决方案


在离线对象里面prev state,有没有说online: false

如果是这样,并且您正在 Android 模拟器中进行开发,如果您使用 API 级别 25 以下的模拟器,则 Wifi 在模拟器上不可用,因此 redux-offline 认为您没有连接并且不会发出初始网络请求。

在 iOS 模拟器中测试 redux-offline 并且一切正常后,这发生在我身上。

希望这可以帮助。


推荐阅读