反应本地Redux离线API响应

时间:2019-02-21 08:31:32

标签: react-native redux-offline

在redux-offline中如何在reducer中查看我的API响应。在我的代码下方,我将我的操作记录在reducer中,我可以看到我的请求数据。在该reducer中,如何预先记录我的响应数据。谢谢

我的代码在这里

action.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 } }
            }
          }
        });

reducer.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;

1 个答案:

答案 0 :(得分:0)

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

也许可以用作以下内容:

case ACTION_CALL_COMMIT: 
  console.log(action.payload);