使用Axios请求从服务器加载Redux存储

时间:2019-12-08 10:25:04

标签: reactjs redux redux-persist

我想使用服务器会话持久化我的redux存储,为此,我实现了一个Transformer来加载数据并使用Axios进行请求以从服务器获取会话数据并将其放入Redux

const SetTransform = createTransform(
  // transform state on its way to being serialized and persisted.
  (inboundState, key) => {
    // convert mySet to an Array.

    return { ...inboundState};
  },
  // transform state being rehydrated
  (outboundState, key) => {
    // convert mySet back to a Set.
    let state=null
    let x=1;
    let test= getUserSession().then(result=>{
        console.log(result);
        state= {...outboundState, ...result.data.user,isLoggedIn:result.data.isConnected}
        console.log(state)
        x=0
        return state;
    })

  }
  // define which reducers this transform gets called for.
);

我在Persist Config中添加了Transformer

const persistConfig = {
  transforms: [SetTransform],
  key: 'root',
  storage: storage,
  stateReconciler: autoMergeLevel2 // see "Merge Process" section for details.
 };

但我遇到的错误是未定义Loaded状态,因为请求是异步的,并且补液功能不会等到请求完成后才开始

那么还有其他实现方法可以使我的商店从用户会话中加载,该怎么做? 谢谢

0 个答案:

没有答案