如何在react-router v4中將history.push傳遞參數params

5 月 17, 2018 | | 0 條留言

this.props.history.push

在導航時,您可以將傳遞道具傳遞給歷史對象

this.props.history.push({
    pathname: '/template',
    search: '?query=abc',
    state: { detail: response.data }
})

Link

或類似的鏈接組件

 My Link 

訪問通過的道具

然後在用/template路由呈現的組件中,可以訪問通過的道具

this.props.location.state.detail

另外請記住,使用道具的歷史或位置對象時,您需要連接組件withRouter。

按照Docs:

withRouter

您可以訪問歷史記錄對象的屬性,並‘s通過withRouter高階組件訪問最接近的匹配項。withRouter 將在每次使用與渲染相同的道具改變路線時重新渲染其組件props: { match, location, history }。