React-Intl injectIntl​​ 不适用于 mergeProps

React-Intl injectIntl not working with mergeProps

我有以下代码可以正常工作

module.exports = injectIntl(redux.connect(mapStateToProps, mapDispatchToProps)(props => {
    document.title = props.intl.formatMessage({ id: "app-name" });

    return (<App {...props} />);
}));

当我将 mergeProps 添加到 redux.connect 时,'intl' 不再存在于我的道具中,我在尝试设置 document.title

时遇到错误

破解代码:

module.exports = injectIntl(redux.connect(mapStateToProps, mapDispatchToProps, mergeProps)(props => {
    document.title = props.intl.formatMessage({ id: "app-name" });

    return (<App {...props} />);
}));

我的 mergeProps 函数:

function mergeProps(stateProps, dispatchProps) {
    const mergeProps = {
        error() {
            alert("throw error");
        },
    };
    return Object.assign({}, stateProps, dispatchProps, mergeProps);
}

在 redux.connect 函数中将 mergeProps 替换为 null 时,没有错误,代码运行正常。

知道为什么 merge props 似乎破坏了 react-intl 注入吗?

通过将 ownProps 传递到合并道具中解决了这个问题。这正是 ownProps 旨在解决的问题,我并不熟悉。

解决方案如下所示:

function mergeProps(stateProps, dispatchProps, ownProps) {
    const mergeProps = {
        error() {
            alert("throw error");
        },
    };
    return Object.assign({}, stateProps, dispatchProps, ownProps, mergeProps);
}