react-router的路径一直重定向到404

huangapple go评论78阅读模式
英文:

react-router paths keep redirecting to 404

问题

我有一个成功跳转到产品管理页面的路由链接:

<Route
  path='/auth/products/product-management'
  render={() => <ProdManagement {...this.props} />}
/>
<Redirect path="*" to="/404" />

ProdManagement组件内部,我有两个按钮,点击时应该带我到其他页面:

const addProduct = () => {
  props.history.push('/auth/products/add-product')
}
const editProduct = () => {
  props.history.push('/auth/products/edit-product')
}

ProdManagement组件的返回路由设置如下:

return (
  <div> 
    <Switch>         
      <Route
        exact
        path='/auth/products/add-product'
        render={() => <AddProduct {...this.props} />}
      />
      <Route
        exact
        path='/auth/products/edit-product'
        render={() => <EditProduct {...this.props} />}
      />
    </Switch>
  </div>
)

但每次我点击按钮时都被发送到404页面。有人知道我在这里做错了什么吗?
我知道addProduct()函数被调用了,因为我在控制台记录了一些东西。

更新:
我将两个路由(添加和编辑)移动到与主路由/auth/products/product-management相同的级别,现在它们可以正常工作了。
不确定为什么,但这修复了它们。

英文:

I have a router link that works successfully taking me to a product-management page:

&lt;Route
  path=&#39;/auth/products/product-management&#39;
  render={() =&gt; &lt;ProdManagement {...this.props} /&gt;}
/&gt;
&lt;Redirect path=&quot;*&quot; to=&quot;/404&quot; /&gt;

Inside the ProdManagement component I have two buttons that onClick should take me to other pages:

const addProduct = () =&gt; {
  props.history.push(&#39;/auth/products/add-product&#39;)
}
const editProduct = () =&gt; {
  props.history.push(&#39;/auth/products/edit-product&#39;)
}

The return for ProdManagement component has the routes set looks like this:

return (
  &lt;div&gt; 
    &lt;Switch&gt;         
      &lt;Route
        exact
        path=&#39;/auth/products/add-product&#39;
        render={() =&gt; &lt;AddProduct {...this.props} /&gt;}
      /&gt;
      &lt;Route
        exact
        path=&#39;/auth/products/edit-product&#39;
        render={() =&gt; &lt;EditProduct {...this.props} /&gt;}
      /&gt;
    &lt;/Switch&gt;
  &lt;/div&gt;
)

But every time I click the buttons I get sent to the 404 page.
Anyone have an idea what I'm doing wrong here?
I know the addProduct() function is being called as I log something to the console.

UPDATE:
I moved the two routes (add & edit) to the same level as the main /auth/products/product-management and they work now.
Not sure why, but it fixed them.

答案1

得分: 1

以下是翻译好的部分:

"The ProdManagement component is rendered on path /auth/products/product-management and when it triggers navigation actions to paths /auth/products/add-product or /auth/products/edit-product the path now no longer matches /auth/products/product-management and ProdManagement is unmounted so the other paths are not mounted and <Redirect path="*" to="/404"/> is what is left to render.

As it seems you've discovered, the easy solution is to just declare the routes where they are always mounted and matchable.

<Switch>
  ... other more specific routes ...

  <Route
    path="/auth/products/product-management"
    component={ProdManagement}
  />
  <Route exact path="/auth/products/add-product" component={AddProduct} /> 
  <Route exact path="/auth/products/edit-product" component={EditProduct} />

  ... other less specific routes ...
 
  <Redirect path="*" to="/404" />
</Switch>
英文:

The ProdManagement component is rendered on path &quot;/auth/products/product-management&quot; and when it triggers navigation actions to paths &quot;/auth/products/add-product&quot; or &quot;/auth/products/edit-product&quot; the path now no longer matches &quot;/auth/products/product-management&quot; and ProdManagement is unmounted so the other paths are not mounted and &lt;Redirect path=&quot;*&quot; to=&quot;/404&quot;/&gt; is what is left to render.

As it seems you've discovered, the easy solution is to just declare the routes where they are always mounted and matchable.

&lt;Switch&gt;
  ... other more specific routes ...

  &lt;Route
    path=&quot;/auth/products/product-management&quot;
    component={ProdManagement}
  /&gt;
  &lt;Route exact path=&quot;/auth/products/add-product&quot; component={AddProduct} /&gt; 
  &lt;Route exact path=&quot;/auth/products/edit-product&quot; component={EditProduct} /&gt;

  ... other less specific routes ...
 
  &lt;Redirect path=&quot;*&quot; to=&quot;/404&quot; /&gt;
&lt;/Switch&gt;

huangapple
  • 本文由 发表于 2023年3月31日 18:49:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75897669.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定