首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Redux ` `connect()`返回一个对象

Redux ` `connect()`返回一个对象
EN

Stack Overflow用户
提问于 2019-06-02 23:53:54
回答 2查看 772关注 0票数 1

我正在使用这些库:

react: 16.8.6
redux: 4.0.1
react-redux: 7.0.3

我有一个简单的组件(Typescript):

import ...;  
import { connect } from 'react-redux';

class ExampleComponent extends React.PureComponent<IProps, {}> {
    constructor(props: any) {
        super(props);
    }

    public render() {
        return (
            <div>
                {this.props.name}
            </div>
        );
    }
}

interface IProps{
    name: string;
}

const mapStateToProps = (state) => {
    return {
        name: state.name
    };
}

export default connect(mapStateToProps)(ExampleComponent);

我有一个父组件:

import ExampleComponent from './ExampleComponent';

export default class App extends React.PureComponent<{}, {}> {
    constructor(props: any) {
        super(props);
    }

    public render() {
        return (
            <div>
                <ExampleComponent />
            </div>
        );
    }
}

由于某些原因,connect(mapStateToProps)(ExampleComponent)返回一个对象,而不是React组件。我收到一个错误:

未捕获错误:不变冲突:元素类型无效:应为字符串(对于内置组件)或类/函数,但得到的是: object

此外,当我尝试执行console.info(typeof connect(mapStateToProps)(ExampleComponent))时,它只将object输出到控制台。

我该如何解决这个问题?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-06-04 20:52:13

我找到了问题所在。这是react-dom库。当redux-dom connect使用可能返回对象的React.memo()方法时,它被错误地设置为16.4.XX。但是react-dom 16.4.XX不知道如何处理这样的对象。

升级到react-dom 16.8.XX解决了这个问题。

票数 0
EN

Stack Overflow用户

发布于 2019-06-03 23:32:41

乍一看,问题似乎出在{this.props.name}上,因为某种原因,它应该是一个字符串并获取一个对象。

请尝试控制“name”

render() {
 const { name } = this.props
 console.log('name', name)

 return (
  //
 )
}

这很可能就是问题所在。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56416629

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档