路由器中的高阶组件需要传递路由器参数

时间:2019-12-19 15:12:27

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

我已经从“ react”创建了以下高阶组件导入React,{useEffect}

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 ... 只要我不在(<Route exact path={pagePath/:someId} component={ContainerComponent} />之间使用HoC,我就具有这些属性,但是当我使用它们时,就无法再访问它们。 在保持高阶组件通用的同时,如何将它们传递给它?

1 个答案:

答案 0 :(得分:2)

尝试一下:

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