即使在使用提供程序或连接后,也无法在“ Connect(App)”的上下文中找到“商店”

时间:2019-08-20 15:40:07

标签: react-native redux expo

我正在尝试在项目中使用redux,使代码像教程一样,但无法正常工作并给出错误

我看到了关于此错误的大多数问题,但是我已经做了他们所说的“灵魂修复”,就像我应该通过提供程序扭曲根组件或使用connect

 Invariant Violation: Could not find "store" in the context of "Connect(App)". Either wrap the root component in a <Provider>, or pass a custom React context provider to <Provider> and the corresponding React context consumer to Connect(App) in connect options.

index.js

import React from 'react';
import {AppRegistry} from 'react-native';
import {Provider} from 'react-redux';
import App from './App';
import { name as appName } from './app.json';
import placesReducer from './src/store/reducers/places';
import{createStore} from 'redux'
import configureStore from './src/store/configureStore';

const store = configureStore();

const RNRedux = () => (
<Provider store={store}>
    <App/>
</Provider>
 );

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

APP.js

import React, { Component } from "react";
import { StyleSheet, View } from "react-native";
import { connect } from "react-redux";

 class App extends Component {

render() {
 return (
  <View style={styles.container}>
   some code
  </View>
  );
 }
 }

const mapStateToProps = state => {
return {
some code
 };
  };

const mapDispatchToProps = dispatch => {
return {
   some code

};
};

export default connect(mapStateToProps, mapDispatchToProps)(App);

我的商店

import {combineReducers, createStore} from 'redux';

import placesReducer from './reducers/places';

const rootReducer = combineReducers({
places: placesReducer
 });

const configureStore =()=> createStore(rootReducer)

 export default configureStore;

2 个答案:

答案 0 :(得分:0)

您必须调用configureStore作为回调吗? 您可以直接这样叫吗?

const configureStore = createStore(rootReducer);

答案 1 :(得分:0)

const configureStore = () => createStore(rootReducer)
在这种情况下,

configureStore是具有createStore返回值的变量,您很可能希望它是一个函数。