大家好,我正在我的 React Native 项目中设置 redux,但在设置时遇到了一些问题,尽管我很确定我直到现在还没有使用过 getState 但是当应用程序运行时我得到错误
<块引用>TypeError:store.getState 不是函数。 (在“store.getState()”中, 'store.getState' 未定义)
我的代码如下
reducers/hireducer.js
a
reducers / index.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;
存储/配置存储
import { combineReducers } from "redux";
import hiReducer from "./hireducer.js";
export default combineReducers({
hi: hiReducer,
});
App.js
import { createStore } from "redux";
import reducers from "../reducers/index";
const configureStore = () => {
return createStore(reducers);
};
export default configureStore;
index.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);
即使我通常在不提供商店的情况下换行,我仍然会遇到相同的错误
答案 0 :(得分:1)
您的 configStore
返回一个函数而不是一个 store 对象。
所以你宁愿在你的应用程序中调用这个函数
<Provider store={configStore()}>
<App {...props} />
</Provider>
或者你从 configStore 创建一个 store 对象,比如
const configureStore = () => {
return createStore(reducers);
};
export default configureStore();