首页 > 解决方案 > TypeError:store.getState 不是函数。(在'store.getState()'中,'store.getState'未定义我该如何解决这个问题?

问题描述

我想连接 redux-saga witdh react-native 但这个错误不断发生......

TypeError:store.getState 不是函数。(在 'store.getState()' 中,'store.getState' 未定义

在此处输入图像描述

警告:失败的道具类型:store提供function 给的道具类型无效Provider,预期object

这是我的代码

  (index.js)


            import {AppRegistry} from 'react-native';
            import Root from './App';
            import {name as appName} from './app.json';

            AppRegistry.registerComponent(appName, () => Root);

(应用程序.js)

    import React from 'react';
    import Store from './store/configureStore'
    import {Provider} from 'react-redux';
    import {App} from './src/index';

    const Root = () => {
      return (
        <Provider store={Store}>
          <App />
        </Provider>
      );
    };

    export default Root;

(src/index.js)

    import React from 'react';
    import Navigator from './Screens/Navigator';
    import styled from 'styled-components/native';

    const App = ({}) => {
      return (
        <Navigator/>
      );
    };

    export {App};

(商店/cofigurestore.js)

     import { applyMiddleware, createStore, compose } from 'redux';
    import createSagaMiddleware from 'redux-saga';
    import { composeWithDevTools } from 'redux-devtools-extension';

    import reducer from '../reducers';
    import rootSaga from '../sagas';

    const Store = () => {
      const sagaMiddleware = createSagaMiddleware();
      const middlewares = [sagaMiddleware];
      const enhancer = process.env.NODE_ENV === 'production'
        ? compose(applyMiddleware(...middlewares))
        : composeWithDevTools(
          applyMiddleware(...middlewares),
        );
      const store = createStore(reducer, enhancer);
      store.sagaTask = sagaMiddleware.run(rootSaga);
      return store;
    };

    export default Store;

(减速器/index.js)

      import { combineReducers } from 'redux';

      import user from './user';
      import post from './post';

      // (이전상태, 액션) => 다음상태
      const rootReducer = (state, action) => {
        switch (action.type) {
          // case HYDRATE:
          //   // console.log('HYDRATE', action);
          //   return action.payload;
          default: {
            const combinedReducer = combineReducers({
              user,
              post,
            });
            return combinedReducer(state, action);
          }
        }
      };

      export default rootReducer;

(圣人/index.js)

    import { all, fork } from 'redux-saga/effects';
    import axios from 'axios';

    import postSaga from './post';
    import userSaga from './user';



    export default function* rootSaga() {
      yield all([
        fork(postSaga),
        fork(userSaga),
      ]);
    }

请帮助我.........我想解决这个问题......但我不知道我该怎么做

标签: javascriptreactjsreact-nativeredux-saga

解决方案


在您App.js身上,您应该将调用Store函数的结果传递给Provider而不是函数本身。

const Root = () => {
  return (
    <Provider store={Store()}>
      <App />
    </Provider>
  );
};

推荐阅读