【问题标题】:store.getState is not a function Redux-persiststore.getState 不是一个函数 Redux-persist
【发布时间】:2019-12-01 19:20:21
【问题描述】:

我正在尝试在我的 React Native 应用程序上实现 Redux-persist。完全按照setup docs,我从这里更改了我的store.js:

import { applyMiddleware, createStore } from 'redux';
import * as thunkMiddleware from 'redux-thunk';

import reducers from '../reducers';

let middlewares = [thunkMiddleware.default];
const store = createStore(reducers, applyMiddleware(...middlewares));

export default store;

到这里:

import { applyMiddleware, createStore } from 'redux';
import * as thunkMiddleware from 'redux-thunk';
import { persistStore, persistReducer } from 'redux-persist';
import AsyncStorage from '@react-native-community/async-storage';

import reducers from '../reducers';

const persistConfig = {
    key: 'root',
    storage: AsyncStorage,
};
const persistedReducer = persistReducer(persistConfig, reducers);

let middlewares = [thunkMiddleware.default];

export default () => {
    let store = createStore(persistedReducer, applyMiddleware(...middlewares));
    let persistor = persistStore(store);
    return { store, persistor };
};

但是现在,我收到了错误 TypeError: store.getState is not a function (In 'store.getState()', 'store.getState' is undefined)

注意:我已经检查了许多有关 stackoverflow 的问题,都存在相同的 store.getState 错误,但它们的具体问题与我的设置不同。

编辑:提供者实现(使用 RNNv2)

import { Navigation } from 'react-native-navigation';
import { Provider } from 'react-redux';

import store from '../../shared/redux/store';
import { registerScreens } from '../view/screens';
import { initialize } from './navigation';

/**
 * Register screens and components for react native navigation
 */
registerScreens({ store, Provider });


const app = () => {
  Navigation.events().registerAppLaunchedListener(() => {
    initialize();
  });
};

export default app;

注册屏幕:

const registerComponentWithRedux = (redux: any) => (
  name: string,
  component: any,
) => {
  Navigation.registerComponentWithRedux(
    name,
    () => component,
    redux.Provider,
    redux.store,
  );
};

export function registerScreens(redux: any) {
  registerComponentWithRedux(redux)(screen, screen.default);
  ...
}

【问题讨论】:

  • 显示你的代码涉及<Provider>
  • 已编辑。我使用的是 RNNv2,所以我不使用直接的 组件,而是使用提供程序来注册我的屏幕。
  • 我对react-native-nav不熟悉,但是导入store的时候,就是导入导出的函数,然后传入registerScreens({ store, Provider });,但是store不是@的输出987654330@ 它是一个函数调用,它返回一个带有键存储和持久化器的对象
  • 很难 100% 了解,但我看不到文件名,试试 registerScreens({ store: store().store, Provider });
  • 很高兴它起作用了,它可能已经清除了错误,但我认为你会遇到更多的错误,我建议一起查看react-native-navigationredux-persist 周围的更多代码,尝试在PersistGate工作

标签: reactjs react-native react-redux react-native-navigation redux-persist


【解决方案1】:

问题在于导出,您正在导出一个返回键中存储的函数。因此,如果您在注册屏幕中更新商店引用,它将起作用。

import returnStoreAndPersistor from '../../shared/redux/store';

const {store} = returnStoreAndPersistor();
registerScreens({ store, Provider });

【讨论】:

    【解决方案2】:

    如果你使用 'redux-persists' 并从 ../store 返回{store, persistor} 试试这个!!!!!!

    import returnStoreAndPersistor from "../../store";
    
    const { store } = returnStoreAndPersistor()
    console.log(store.getState())
    

    【讨论】:

      【解决方案3】:

      对于遇到此问题且正在使用 Typescript 且“getState()”上的唯一属性是“_persist”的任何人,我的问题是由于类型将组合减速器的类型设置为“any”而引起的将您的商店提供给 persistStore 时出错:

      Type AnyAction is not assignable to type {your action type}

      当我解决了上述问题后,getState 问题就解决了。

      我通过这样做解决了这个问题:

      const _ = (state: any = 0, _: AnyAction) => state;
      const root = combineReducers({
          _,
          applicationStatusReducer,
          ...
      });
      

      如果你只是添加一个动作类型为“AnyAction”的空reducer,它似乎可以解决问题(尽管是以一种hacky/实际上并没有解决底层问题的方式)。

      【讨论】:

        【解决方案4】:

        发生此问题是因为此导出默认方法而不是您执行此操作 可以这样。

        export let store = createStore(persistedReducer, applyMiddleware(...middleware));
        
        
        export let persistor = persistStore(store);
        

        抱歉给您带来不便

        【讨论】:

          【解决方案5】:
          const store = createStore(persistedReducer, applyMiddleware(...middlewares));
          const persistor = persistStore(store);
          export { store, persistor };
          

          然后

          import { store, persistor } from '../../shared/redux/store';
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2022-01-06
            • 1970-01-01
            • 1970-01-01
            • 2021-05-15
            • 1970-01-01
            • 2020-02-26
            • 1970-01-01
            相关资源
            最近更新 更多