微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过在类组件中多次导出来解决问题

如何解决通过在类组件中多次导出来解决问题

以前,在我的类组件中,redux connect看起来像这样。这项工作没有任何问题。

//File: MyComponent.jsx
    //in below code this.props.selectProducts is not undefined and this works fine
        const mapStatetoProps = null;
        const mapActionsToProps = {
            selectProducts
        };
        
        export default connect(
            mapStatetoProps,mapActionsToProps
        )(MyComponent);

此后,我不得不使用名为notistack的组件来显示小吃栏,然后将其认导出,而不是现有的redux connect。现在我面临的问题是道具不存在。

const mapStatetoProps = null;
const mapActionsToProps = {
    selectProducts
};

//Now this.props.selectProducts is undefined which is part of redux store
//But this.props.enqueueSnackbar("Preference saved."); this works. which is part of withSnackbar
export const ConnectedList = connect(
    mapStatetoProps,mapActionsToProps
)(MyComponent);
export default withSnackbar(MyComponent);

如果我将redux作为认连接,则无法访问this.props.enqueueSnackbar()。任何帮助将非常感激。谢谢

解决方法

只需在withSnackbar返回的组件上使用connect并使用它:

const ConnectedList = connect(
    mapStateToProps,mapActionsToProps
)(MyComponent);
export default withSnackbar(ConnectedList);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。