【问题标题】:React-Intl injectIntl not working with mergePropsReact-Intl injectionIntl 不适用于 mergeProps
【发布时间】:2019-01-18 05:14:41
【问题描述】:

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

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

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

当我将 mergeProps 添加到 redux.connect 时,'intl' 不再存在于我的 props 中,并且在尝试设置 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函数中用null替换mergeProps时,没有报错,代码运行正常。

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

【问题讨论】:

    标签: reactjs redux react-redux react-intl


    【解决方案1】:

    通过将 ownProps 传递给 merge props 解决了这个问题。这正是 ownProps 旨在解决的问题,我并不熟悉。

    解决方案如下:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      • 2015-07-05
      • 2017-12-03
      • 1970-01-01
      • 2017-11-14
      • 2016-10-26
      • 2019-08-15
      相关资源
      最近更新 更多