ReferenceError:初始化之前无法访问存储

时间:2020-08-18 04:12:11

标签: javascript reactjs typescript react-redux axios

我有这个store/index.ts

export const storeRedux = createStore(
  persistReducer(ReduxPersistConfig, rootReducer),
  composeEnhancers(applyMiddleware(sagaMiddleware))
);

并具有此实现以访问组件外部的令牌

//api.ts

import axios from 'axios';
import { storeRedux } from 'store';

let token;

const listener = () => {
  token = storeRedux.getState().user.token;
};

storeRedux.subscribe(listener);

// console.log(() => getAPI());

export default axios.create({
  baseURL: 'http://localhost:5000/api/security',
  headers: {
    Authorization: `Bearer ${token}`,
  },
 
});

如果我要添加setTimeout来包装getState(),那么一切都会好起来的。

还有其他解决方案,因为商店已经像这样被分开了 article

0 个答案:

没有答案