Redux store.getState() 返回初始状态

时间:2021-01-17 12:41:50

标签: reactjs redux

我在 React Native 应用程序中使用 Redux。 我想在反应组件之外获得实际状态

我的文件:

import memoize from 'lodash.memoize';
import i18n from 'i18n-js';
import * as Localization from 'expo-localization';
import { I18nManager } from 'react-native';
import configureStore from "../store/configureStore"

const store = configureStore();

export const translationGetters = {
    'en-US': () => require('../localizations/en.json'),
    'en-GB': () => require('../localizations/en.json'),
    'ru-RU': () => require('../localizations/ru.json'),
};

export const IMLocalized = memoize(
    (key, config) =>
        i18n.t(key, config).includes('missing') ? key : i18n.t(key, config),
    (key, config) => (config ? key + JSON.stringify(config) : key),
);

export const init = () => {
    let localeLanguageTag = Localization.locale;
    let isRTL = Localization.isRTL;
    IMLocalized.cache.clear();
    I18nManager.forceRTL(isRTL);
    i18n.translations = {
        [localeLanguageTag]: translationGetters[localeLanguageTag](),
    };
    i18n.locale = localeLanguageTag;
    getCodeLocal();
};

const getCodeLocal = () => {
    console.log('what is currently in store', store.getState())
}

我的文件 configureStore.js 用于创建商店

import { createStore, combineReducers } from 'redux';
import SettingsReducer from "./reducers/SettingsReducer";
import SheetReducer from "./reducers/SheetReducer";

const rootReducer = combineReducers(
    {
        schedule: SettingsReducer,
        sheet: SheetReducer
    }
);

const configureStore = () => {
    return createStore(rootReducer);
}

export default configureStore;

在调用 getCodeLocal 后,我总是得到初始状态。 我如何获得当前状态?

0 个答案:

没有答案