1

在 redux-offline 如何在 reducer 中查看我的 API 响应。在我的代码下面,我将我的操作记录在减速器中,我可以看到我的请求数据。在那个减速器中如何记录我的响应数据。提前谢谢

我的代码在这里

动作.js

 export const sample = (requestbodydata) => ({

          type: 'ACTION_CALL',
          payload: { requestbodydata },
          meta: {
            offline: {
              // the network action to execute:
              effect: { url: 'http://localhost:5000/api/v1/test/data', method: 'POST',  body: requestbodydata , headers: { 'content-type': 'application/x-www-form-urlencoded' } },
              // action to dispatch when effect succeeds:
              commit: { type: 'ACTION_CALL_COMMIT', meta: { requestbodydata } },
              // action to dispatch if network action fails permanently:
              rollback: { type: 'ACTION_CALL_ROLLBACK', meta: { requestbodydata } }
            }
          }
        });

减速器.js

export default function reducer(state = {}, action) {
    switch (action.type) {        
        case 'ACTION_CALL':
        console.log('get response'+JSON.stringify(action));

        case 'ACTION_CALL_COMMIT':
        console.log('ACTION_CALL_COMMIT')
        console.log('After commit response'+JSON.stringify(action));

        case 'ACTION_CALL_ROLLBACK':
        console.log('ACTION_CALL_ROLLBACK')


        default:
            return state
    }
}

store.js

    import { applyMiddleware, createStore, compose } from 'redux';
    import { offline } from '@redux-offline/redux-offline';
    import offlineConfig from '@redux-offline/redux-offline/lib/defaults';
    import reducer from './reducer';


const store = createStore(
  reducer,
  {},
  compose(
    applyMiddleware(thunk),
    offline(offlineConfig)
  )
);

export default store;
4

1 回答 1

0

https://github.com/redux-offline/redux-offline/issues/63提到提交会自动将其 action.payload 设置为效果的响应。

也许这可以用作以下内容:

case ACTION_CALL_COMMIT: 
  console.log(action.payload);
于 2021-03-09T08:23:38.210 回答