如何从辅助类或函数访问 redux 存储

Inu*_*aha 2 reactjs react-native redux

我将访问令牌存储在 Redux 存储中,并且希望将其与需要身份验证的每个 API 请求一起发送。我有一个 API 函数类。这是一种图书馆。我不确定如何从 API 类访问令牌。

有人可以帮忙解决这个问题吗?

小智 6

您必须将商店导入到您需要从各自的索引文件访问商店的文件中,我已经这样做了

import { store } from '../index';

 store.subscribe(() => {
      store.getState();
      token = store.getState().Login.token; //access token here
 })
Run Code Online (Sandbox Code Playgroud)

在index.android.js/index.ios.js我已经导出商店

export const store = createStore(
  RootReducer,
  undefined,
  // the ordering of middleware is significant.
  applyMiddleware(thunk),
);
Run Code Online (Sandbox Code Playgroud)