首页 > 解决方案 > 在非 React 组件中访问 React persistStore

问题描述

你能帮我了解如何访问 Redux 持久存储非反应组件吗?Store.js 和非反应组件如下 -

Store.js

const persistedReducer = persistReducer(persistConfig, rootReducer);

function configureStore(initialState) {
    const sagaMiddleware = createSagaMiddleware();
    const store = createStore(
        persistedReducer,
        initialState,
        bindMiddleware([sagaMiddleware])
    );

    store.sagaTask = sagaMiddleware.run(rootSaga);
    return store;
}

export default configureStore;

非反应成分 -

export const customHeaders = {
    Accept: 'application/json',
    Authorization: authorization_prefix + token //i want to access the token here from the above persist store.
};


export default axios.create({
    baseUrl,
    headers: customHeaders,
});

我尝试了下面的代码,但它没有用

import configureStore from 'path to configureStore'
const { store } = configureStore();
console.log(store.getState()); //Error : TypeError: Object(...) is not a function

标签: reactjsreduxreact-reduxnext.jsredux-persist

解决方案


推荐阅读