在“ Connect(AppComponent)”的上下文中找不到“商店”

时间:2019-02-06 13:44:47

标签: javascript reactjs react-native react-redux

我遇到此错误,我用谷歌搜索了解决方案,但似乎一切都应正确设置。

完整错误是: “不变违规:在“ Connect(AppComponent)”的上下文中找不到“ store”。将根组件包装在中,或者将自定义React上下文提供程序传递给Connect(AppComponent),并将相应的React上下文使用者传递给connect选项中的Connect(AppComponent)。

此错误位于:在RCTView(在View.js :)的View(AppContainer.js:98)在RCTView(View.js:45)的Connect(AppComponent)(在renderApplication.js:34): 45)在AppContainer的View(在AppContainer.js:115)(在renderApplication.js:33)

这是我的代码:

index.js

import { Provider } from 'react-redux'
import { createStore } from 'redux';

import App from './app/App';
import appState from './app/redux/reducers';

import {name as appName} from './app.json';

let store = createStore(appState);
export default class AppRoot extends Component {
  render() {
    return (
      <Provider store={store}>
        <App/>
      </Provider>
    );
  }
}

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

App.js

//import...

class AppComponent extends Component {
  //code..
}

const mapStateToProps = (state, props) => {
  return {}
};

const mapDispatchToProps = (dispatch, props) => {
  return {
    onFavoriteChange: (id, type) => {
      switch(type){
        //cases...
      }
    }
  }
};

const App = connect(
  mapStateToProps,
  mapDispatchToProps
)(AppComponent);

export default App;

index.js(在/ reducers中)

import { combineReducers } from 'redux';

//imports...

let appState = combineReducers({
    //all import...
});

export default appState;

1 个答案:

答案 0 :(得分:1)

在根目录注册的App组件不是AppRoot。这意味着React渲染App而不用Provider来提供存储。然后connect HOC会引发错误。您必须将呼叫更新为:

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