React Router: 嵌套组件中的URL重定向问题

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

React Router: Issue with URL redirection in nested components

问题

我在我的React应用中使用React Router(v5.3.4)时遇到了一个URL重定向的问题。当我点击一个产品时,我会被重定向到产品页面,URL看起来像这样:“http://localhost:3000/product/34”。

然而,当我从该页面点击一个相关的产品时,它不是更新URL为“http://localhost:3000/product/35”,而是将新URL附加到现有的URL上,导致了“http://localhost:3000/product/34/product/35”。

我尝试了一些解决方案,包括使用React Router的Link组件,修改URL使用window.location.href,以及使用useNavigate钩子,但都没有解决这个问题。

以下是来自我的Product组件的相关代码片段:

import React from 'react';
import { Link } from 'react-router-dom';

const Product = ({ product }) => {
  return (
    <Link to={`product/${product.id}`}>
      {/* 产品内容 */}
    </Link>
  );
};

export default Product;

我还尝试通过修改Link组件的to属性,仅使用产品ID来排查问题:

import React from 'react';
import { Link } from 'react-router-dom';

const Product = ({ product }) => {
  return (
    <Link to={`${product.id}`}>
      {/* 产品内容 */}
    </Link>
  );
};

export default Product;

现在,当我点击相关产品时,URL变成了“http://localhost:3000/product/34/35”,而不是最初的“http://localhost:3000/product/34/product/35”。

是否有人能提供一些建议或替代方法?

英文:

I'm facing an issue with URL redirection in my React app using React Router (v5.3.4). When I click on a product, I'm redirected to the product's page with a URL like &quot;http://localhost:3000/product/34&quot;.

However, when I click on a related product from that page, instead of updating the URL to &quot;http://localhost:3000/product/35&quot;,
it appends the new URL to the existing one, resulting in &quot;http://localhost:3000/product/34/product/35&quot;.

I've tried a few solutions including using Link component from React Router, modifying the URL using window.location.href, and utilizing the useNavigate hook, but none of them have resolved the issue.

Here's the relevant code snippet from my Product component:

import React from &#39;react&#39;;
import { Link } from &#39;react-router-dom&#39;;

const Product = ({ product }) =&gt; {
  return (
    &lt;Link to={`product/${product.id}`}&gt;
      {/* Product content */}
    &lt;/Link&gt;
  );
};

export default Product;

I also tried to troubleshoot the problem by modifying the code to use only the product ID in the to attribute of the Link component:

import React from &#39;react&#39;;
import { Link } from &#39;react-router-dom&#39;;

const Product = ({ product }) =&gt; {
  return (
    &lt;Link to={`${product.id}`}&gt;
      {/* Product content */}
    &lt;/Link&gt;
  );
};

export default Product;

Now, when I click on a related product, the URL becomes &quot;http://localhost:3000/product/34/35&quot; instead of &quot;http://localhost:3000/product/34/product/35&quot; as it was initially.

Could anyone provide any guidance/suggestions or alternative approaches?

答案1

得分: 1

使用绝对路径,例如以前导的 &quot;/&quot; 字符开头,以确保新路径不会作为相对链接附加到当前URL路径。

示例:

const Product = ({ product }) =&gt; {
  return (
    &lt;Link to={`/product/${product.id}`}&gt;
      {/* 产品内容 */}
    &lt;/Link&gt;
  );
};
import { Link, generatePath } from &#39;react-router-dom&#39;;

const Product = ({ product }) =&gt; {
  return (
    &lt;Link to={generatePath(&quot;/product/:productId&quot;, { productId: product.id })}&gt;
      {/* 产品内容 */}
    &lt;/Link&gt;
  );
};
英文:

Use an absolute path, e.g. one that starts with a leading &quot;/&quot; character so the new path is not appended to the current URL path as a relative link.

Examples:

const Product = ({ product }) =&gt; {
  return (
    &lt;Link to={`/product/${product.id}`}&gt;
      {/* Product content */}
    &lt;/Link&gt;
  );
};
import { Link, generatePath } from &#39;react-router-dom&#39;;

const Product = ({ product }) =&gt; {
  return (
    &lt;Link to={generatePath(&quot;/product/:productId&quot;, { productId: product.id })}&gt;
      {/* Product content */}
    &lt;/Link&gt;
  );
};

huangapple
  • 本文由 发表于 2023年6月22日 05:25:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527252.html
匿名

发表评论

匿名网友

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

确定