【问题标题】:Error: The Component for route 'HomeScreen' must be a React Component while using Redux connect()错误:使用 Redux connect() 时,路由“HomeScreen”的组件必须是 React 组件
【发布时间】:2019-09-11 07:19:43
【问题描述】:

我收到此错误 - “在使用 Redux connect() 时,路由 'HomeScreen' 的组件必须是 React 组件” 在应用 Redux connect() 和删除 connect() 时应用在本机反应中运行良好。

下面我已经提到了代码:

import React, { Component, Fragment } from 'react';
import { View, Text } from 'react-native';
import {connect} from 'react-redux';

class Sample extends Component {
    constructor(props) {
        super(props);
        this.state = {
            loading: true,
            dataSource: {}
        };
    }
    render() {
        return (
            <View>
                <Text>Hi</Text>
            </View>
        )
    }
}


const container = connect()(Sample);
export default container;

我也尝试将 React Redux 版本从 7.0.1 降级到 6.0.1,但我仍然面临同样的问题。

我也试过Solution,但问题仍然存在。

【问题讨论】:

  • 你能提供你的导入吗?也很高兴看到 Home 组件
  • 这是我的导入:import React, { Fragment } from 'react';导入 { SafeAreaView, StyleSheet, ScrollView, View, Text, StatusBar, } from 'react-native';从 'react-native/Libraries/NewAppScreen' 导入 { Header, LearnMoreLinks, Colors, DebugInstructions, ReloadInstructions, };从'./src/Components/Home'导入主页;
  • 这些导入是否属于您使用 redux connect 导出组件的模块?因为您正在导入 Home,然后根据您的问题示例使用 redux connect 再次导出它
  • 这是我的 Home 组件: class Home extends Component { constructor(props) { super(props); } componentDidMount() { this.props.getTopCategoryDe​​tails();} render() { const { topCategory } = this.props; return ( Hi ) } }导出默认连接(mapStateToProps, mapDispatchToProps)(Home);
  • 你在你家所在的模块中导入connect了吗?

标签: reactjs react-native redux react-redux react-native-android


【解决方案1】:

最后,我通过将 react-redux 从 7.0.2 降级到 5.1.1 解决了我的问题,问题是在 react-redux 7.0.2 中 connect() 方法返回对象,但在 react-navigation 2.18.2 中它假设我猜是功能,所以类型不匹配,因此我必须将 react-redux 降级到 5.1.1。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    相关资源
    最近更新 更多