【问题标题】:Could not find "store" in the context of "Connect(App)"在“Connect(App)”的上下文中找不到“store”
【发布时间】:2019-11-27 04:46:37
【问题描述】:

当我尝试获取数据和componentDidMount 时出现此错误。我该如何解决这个问题,它是在我的应用程序中获取数据的最佳位置吗?

不变违规:在“Connect(App)”的上下文中找不到“store”。要么将根组件包装在 a 中,要么将自定义的 React 上下文提供者传递给连接选项中的 Connect(App),并将相应的 React 上下文消费者传递给。

import React, { Component } from "react";
import {
  Platform,
  StatusBar,
  StyleSheet,
  View,
  ImageBackground
} from "react-native";
import ApolloClient from "apollo-boost";
import { ApolloProvider } from "react-apollo";
import { Provider } from "react-redux";
import { PersistGate } from "redux-persist/integration/react";
import { connect } from "react-redux";
import store from "./config/configureStore";
import { fetchData, loadState } from "./actions/videos";
import AppNavigator from "./navigation/AppNavigator";
import NavigationService from "./navigation/actions";

const Root = () => (
  <Provider store={store}>
    <PersistGate loading={null} persistor={persistor}>
      <AppNavigator
        ref={navigatorRef => {
          NavigationService.setTopLevelNavigator(navigatorRef);
        }}
      />
    </PersistGate>
  </Provider>
);

class App extends React.Component {
  componentDidMount() {
    const { fetchData } = this.props;
    fetchData();
  }

  render() {
    return (
      <ImageBackground
        source={require("./assets/images/TC_background.jpg")}
        style={styles.container}
      >
        <Root />
      </ImageBackground>
    );
  }
}

const mapDispatchToProps = dispatch => ({
  fetchData: () => dispatch(fetchData())
});
const mapStateToProps = state => {};
export default connect(
  mapStateToProps,
  mapDispatchToProps
)(App);

var styles = StyleSheet.create({
  container: {
    flex: 1,
    fontFamily: "lato-regular",
    backgroundColor: "transparent"
  },
  backgroundImage: {
    flex: 1,
    resizeMode: "cover"
  }
});

【问题讨论】:

  • 我认为你的设置是错误的。您应该在通过商店的主要 App 组件中使用 Provider。然后进入你的 Root 组件,你使用 connect 函数来访问存储。您可以在此答案中找到更多详细信息-> stackoverflow.com/questions/41892553/…

标签: reactjs react-native react-redux


【解决方案1】:

你有reducer和store,是吗?

如果你有这样的尝试

另外,你可以这样写你的调度方法

export default connect(mapStateToProps, mapDispatchToProps)(App)

你也可以这样写你的调度方法

const mapDispatchToProps = {
 fetchData,
}

【讨论】:

    猜你喜欢
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 2017-11-11
    相关资源
    最近更新 更多