簡體   English   中英

React 組件未在 history.push 上重新加載

[英]React component not reloading on history.push

我有一個應用程序,其路由定義如下:

<Route exact path="/:category/:product" component={ProductComponent} />

在類別頁面上, "/:category每個產品都有兩個按鈕,“快速查看”和“完整查看”,點擊它們我想移動到產品頁面,但也設置視圖狀態,如setView('quick')setView('full')

我嘗試通過使用 onclick 按鈕和以下代碼來解決這個問題:

   () => {
        setView('quick')
        history.push(`/${category.slug}/${product.slug}`)
    }

這里兩者都被調用,history api 更改了 url,但組件不會為該 url 加載。 我無法使用 react-router 的<Link>組件,因為我需要在進入產品頁面之前設置視圖。

如何使用 react-router 的歷史記錄來像<Link>那樣執行頁面更改?

嘗試這個
移除組件並使用渲染

<Route
  exact
  path="/:category/:product"
  render={({ match }) => <ProductComponent key={match.params.product || 'empty'} />}
/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM