错误:在使用Redux connect()时,路由“HomeScreen”的组件必须是React组件。

13 浏览
0 Comments

错误:在使用Redux connect()时,路由“HomeScreen”的组件必须是React组件。

在使用Redux connect()时,我遇到了这个错误 - "The Component for route 'HomeScreen' must be a React Component while using Redux connect()"。当我移除connect()时,React Native的应用程序运行正常。

下面是我提到的代码:

import React, { Component } 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 (
            
                Hi
            
        )
    }
}
const container = connect()(Sample);
export default container;

我还尝试将React Redux版本从7.0.1降级到6.0.1,但问题仍然存在。

我还尝试了解决方案,但问题仍然存在。

0
0 Comments

最近,我遇到了一个问题,通过将react-redux版本从7.0.2降级为5.1.1,最终解决了这个问题。问题出在react-redux 7.0.2版本中的connect()方法返回的是一个对象,而在react-navigation 2.18.2版本中,它被认为是一个函数,我猜这可能导致了类型不匹配的问题,因此我不得不将react-redux降级到5.1.1版本。

解决这个问题的方法就是将react-redux版本降级到5.1.1。这样,connect()方法将返回一个函数,而不是一个对象,与react-navigation的要求匹配。

以下是我用于降级react-redux的步骤:

1. 打开终端或命令提示符,并导航到你的项目目录。

2. 运行以下命令来安装react-redux的5.1.1版本:

npm install react-redux@5.1.1

3. 等待安装完成后,重新启动你的项目。

这样,你的问题应该就会得到解决了。现在,connect()方法将返回一个函数,与react-navigation的要求保持一致,不再产生类型不匹配的错误。

希望这篇文章对你有所帮助!如果你遇到了类似的问题,不妨尝试一下降级react-redux版本来解决。祝你好运!

0