Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
834 views
in Technique[技术] by (71.8m points)

reactjs - TypeError: store.getState is not a function. (In 'store.getState()', 'store.getState' is undefined)

Hi guys i was setting up redux in my react native project but facing some issues while setup even though i am pretty sure i haven't used getState till now but as ssons as app runs i get error

TypeError: store.getState is not a function. (In 'store.getState()', 'store.getState' is undefined)

my code is as

reducers / hireducer.js

import { HI } from "../constants";
const initialState = {
  count: 0,
};
const countReducer = (state = initialState, action) => {
  switch (action.type) {
    case HI:
      return {
        ...state,
        count: action.payload,
      };
    default:
      return state;
  }
};
export default countReducer;

reducers / index.js

import { combineReducers } from "redux";
import hiReducer from "./hireducer.js";
export default combineReducers({
  hi: hiReducer,
});

store / configStore

import { createStore } from "redux";
import reducers from "../reducers/index";

const configureStore = () => {
  return createStore(reducers);
};
export default configureStore;

App.js

import "react-native-gesture-handler";
import React from "react";
import { NavigationContainer } from "@react-navigation/native";
import AuthNavigator from "./navigations/AuthNavigation";
import { withHOC } from "./index.js";
function App() {
  return (
    <NavigationContainer>
      <AuthNavigator />
    </NavigationContainer>
  );
}

export default withHOC(App);

index.js

import React from "react";
import { Provider } from "react-redux";
import configStore from "./redux/store/configStore";
export const withHOC = (App) => (props) => {
  return (
    <Provider store={configStore}>
      <App {...props} />
    </Provider>
  );
};

even though if i normally wrap in without providing store i still get the same error


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your configStore returns a function instead of a store object.

So you rather call the function in your app like

<Provider store={configStore()}>
  <App {...props} />
</Provider>

Or you create a store object from configStore like

const configureStore = () => {
  return createStore(reducers);
};
export default configureStore();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...