【问题标题】:Higher order component in router needs to pass router parameters路由器中的高阶组件需要传递路由器参数
【发布时间】:2020-04-12 05:04:48
【问题描述】:

我创建了以下高阶组件import React, { useEffect } from 'react'

export const withCall = (WrappedComponent, calllName) => {

    const HOC = () => {
        useEffect(() =>{report(calllName)}, [])
        return <WrappedComponent />
    }

    return HOC
}

还有这条路线:

<Route exact path={pagePath/:someId} component={withCall(ContainerComponent, 'any_call_name')} />

在 ContainerComponent 我试图访问this.props.match.params... 只要我不在两者之间使用 HoC (&lt;Route exact path={pagePath/:someId} component={ContainerComponent} /&gt;),我就有这些属性,但是当我使用它时,我就无法再访问它们了。 如何在保持高阶组件通用的同时将它们传递给它?

【问题讨论】:

    标签: reactjs react-router parameter-passing higher-order-components


    【解决方案1】:

    试试这个:

    const HOC = (props) => {
        useEffect(() =>{report(calllName)}, [])
        return <WrappedComponent {...props} />
    }
    

    【讨论】:

    • 欢迎来到 Stack Overflow。请解释为什么这会回答问题。
    猜你喜欢
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 2020-06-13
    相关资源
    最近更新 更多