【问题标题】:Getting store not found error in React Native Redux在 React Native Redux 中获取 store not found 错误
【发布时间】:2018-08-10 08:52:05
【问题描述】:

我的App.js 文件是:

import React, { Component } from 'react';
import { Provider, connect } from 'react-redux';
import { createStore, bindActionCreators } from 'redux';
import {Platform, StyleSheet, Text, View} from 'react-native';
import reducers from './reducers';

class App extends Component {
  render() {

    return (
      <Provider store={createStore(reducers)}>
        <View>
          <Text>12345</Text>
        </View>
      </Provider>
    );
  }
}

const mapStateToProps = state => {
  console.log(state);
};

export default connect(mapStateToProps)(App);

我收到以下错误:

Invariant Violation: Could not find "store" in either the context or props of "Connect(App)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(App)".

我不知道错误在哪里。我该如何解决?

【问题讨论】:

  • 为什么要使用 connect(mapStateToProps)(App) 导出应用程序;它不是必需的。删除它,我认为它可以正常工作

标签: react-native redux react-redux


【解决方案1】:

您的方法几乎正确,但仍需要调整。问题是因为connect 试图找到 redux 存储实例,但没有关于它的参考。

您需要移动 connect 并将子组件包装起来而不是根。

import React, { Component } from 'react';
import { Provider, connect } from 'react-redux';
import { createStore, bindActionCreators } from 'redux';
import {Platform, StyleSheet, Text, View} from 'react-native';
import reducers from './reducers';

const Content = (props) => (
  <View>
    <Text>12345</Text>
  </View>
);

const mapStateToProps = state => {
  console.log(state);
};

// Use connect enhancer, when the parent is wrapped with <Provider>
const EnhancedContent = connect(mapStateToProps)(Content);

class App extends Component {
  render() {
    return (
      <Provider store={createStore(reducers)}>
        <EnhancedContent />
      </Provider>
    );
  }
}

export default App;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-25
    • 2018-05-13
    • 2013-10-05
    • 1970-01-01
    • 2015-11-22
    • 1970-01-01
    • 2019-10-30
    • 1970-01-01
    相关资源
    最近更新 更多