Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何访问redux工具箱中react组件外部的react redux存储?_Javascript_Reactjs_Redux_React Redux_Redux Toolkit - Fatal编程技术网

Javascript 如何访问redux工具箱中react组件外部的react redux存储?

Javascript 如何访问redux工具箱中react组件外部的react redux存储?,javascript,reactjs,redux,react-redux,redux-toolkit,Javascript,Reactjs,Redux,React Redux,Redux Toolkit,我正在尝试访问我的助手函数中的redux存储。 我的商店代码如下所示: import { combineReducers, configureStore } from '@reduxjs/toolkit'; import createSagaMiddleware from 'redux-saga'; import counterReducer from 'app/Components/Counter/counterSlice'; import languageReducer from 'redu

我正在尝试访问我的助手函数中的redux存储。 我的商店代码如下所示:

import { combineReducers, configureStore } from '@reduxjs/toolkit';
import createSagaMiddleware from 'redux-saga';
import counterReducer from 'app/Components/Counter/counterSlice';
import languageReducer from 'redux/features/app_language/language.slice';
import loginReducer, {
    currentUserReducer,
    currentUserIdReducer,
} from 'redux/features/app_login/loginSlice';
import rootSaga from 'redux/sagas';
import emailEditorReducer from 'redux/features/email_editor';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';

const configureCustomStore = () => {
    const sagaMiddleware = createSagaMiddleware();

    const persistConfig = {
        key: 'root',
        storage,
    };

    const rootReducer = combineReducers({
        counter: counterReducer,
        app_language: languageReducer,
        isAuthenticated: loginReducer,
        currentUser: currentUserReducer,
        currentUserId: currentUserIdReducer,
        emailEditor: emailEditorReducer,
    });

    const persistedReducer = persistReducer(persistConfig, rootReducer);

    const store = configureStore({
        reducer: persistedReducer,
        middleware: (getDefaultMiddleware) =>
            getDefaultMiddleware({
                serializableCheck: false,
            }).concat(sagaMiddleware),
    });

    const persistor = persistStore(store);
    sagaMiddleware.run(rootSaga);
    return { store, persistor };
};
export default configureCustomStore();

export const { store, persistor } = configureCustomStore();
console.log(store.getState(), 'State');

正如您在最后两行代码中所看到的,我正在尝试console.log状态,并且获得了预期的状态。但是,当我在助手函数中导入存储时,我得到的是未定义的。下面是我是怎么做的

import storeSaga from 'redux/store/store';
const { store } = storeSaga;
我得到的错误是:

TypeError: Cannot destructure property 'store' of 'redux_store_store__WEBPACK_IMPORTED_MODULE_1__.default' as it is undefined.
我一直在寻找解决方案,我最终得到了几乎相同的问题,答案也在下一篇评论中提到,但老实说,我不明白

我可能做了些错事,为了进口我尽了我所能。 有关参考信息,请查看下面的my index.js文件:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';

import { Provider } from 'react-redux';
import { I18nextProvider } from 'react-i18next';
import * as serviceWorker from './serviceWorker';
import i18n from './_helpers/utils/i18n';
import storeSaga from 'redux/store/store';
const { store, persistor } = storeSaga;
import App from './app/Pages/App/App';

import { PersistGate } from 'redux-persist/integration/react';

ReactDOM.render(
    <React.StrictMode>
        <Provider store={store}>
            <PersistGate persistor={persistor}>
                <I18nextProvider i18n={i18n}>
                    <App />
                </I18nextProvider>
            </PersistGate>
        </Provider>
    </React.StrictMode>,
    document.getElementById('root'),
);

serviceWorker.unregister();
从“React”导入React;
从“react dom”导入react dom;
导入“./index.css”;
从'react redux'导入{Provider};
从'react-I18nextProvider'导入{I18nextProvider};
将*作为serviceWorker从“/serviceWorker”导入;
从“/”helpers/utils/i18n”导入i18n;
从“redux/store/store”导入storeSaga;
const{store,persistor}=storeSaga;
从“./App/Pages/App/App”导入应用程序;
从'redux persist/integration/react'导入{PersistGate};
ReactDOM.render(
,
document.getElementById('root'),
);
serviceWorker.unregister();
我非常感谢你的帮助。提前感谢。

导出默认配置CustomStore();
export const{store,persistor}=configureCustomStore();
您正在创建两个独立的商店

此外,在导入时,您会导入其中的第一个,并将其命名为
storeSaga
,即使这与saga无关

取消默认的导出,并从'redux/store/store'执行
import{store}无处不在。
这会让你更清楚地知道你在做什么

如果在此之后仍有错误,则需要在某个位置进行循环导入,这意味着此文件从同时导入此文件的文件导入(可能中间还有第三个文件),形成一个循环。JavaScript无法处理该问题。

导出默认配置CustomStore();
export const{store,persistor}=configureCustomStore();
您正在创建两个独立的商店

此外,在导入时,您会导入其中的第一个,并将其命名为
storeSaga
,即使这与saga无关

取消默认的导出,并从'redux/store/store'执行
import{store}无处不在。
这会让你更清楚地知道你在做什么


如果在此之后仍有错误,则需要在某个位置进行循环导入,这意味着此文件从同时导入此文件的文件导入(可能中间还有第三个文件),形成一个循环。JavaScript无法处理此问题。

非常感谢您的回复。我按照上面提到的那样做了,现在我得到了这个错误:ReferenceError:在初始化之前无法访问“store”。这是因为你提到的循环进口吗?是的,很有可能。你需要打破这个圈子。通常,将多个文件使用的内容移动到第三个文件中会有所帮助。但最后,你必须在那里进行实验。非常感谢你的回复。我按照上面提到的那样做了,现在我得到了这个错误:ReferenceError:在初始化之前无法访问“store”。这是因为你提到的循环进口吗?是的,很有可能。你需要打破这个圈子。通常,将多个文件使用的内容移动到第三个文件中会有所帮助。但最终,你必须在那里进行实验。