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

问题描述 投票:0回答:1

我已经创建了以下更高阶的组件,从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,我就具有这些属性,但是当我使用它时,我将无法再访问它们。如何在保持高阶组件通用的同时将它们传递给它?

reactjs react-router parameter-passing higher-order-components
1个回答
2
投票

尝试一下:

const HOC = (props) => {
    useEffect(() =>{report(calllName)}, [])
    return <WrappedComponent {...props} />
}
© www.soinside.com 2019 - 2024. All rights reserved.