所有我的React.js项目中的链接都变得无法点击。

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

All my links in my react js projects have become unclickable

问题

I am using react v17 and react-router-dom v6 in my project, and my Route component uses element instead of component. My problem begins when I try to get and show data from the server, and I use the useEffect hook.

<Routes>
  <Route exact path="/" element={<HomeFour />} />
  <Route path="/articles/:page" element={<Article />} exact />
<Routes>

Example of a link:

<a
  className="nav-link active"
  id="pills-home-tab"
  data-bs-toggle="pill"
  href="#pills-home"
  role="tab"
  aria-controls="pills-home" 
  aria-selected="true"
>
  Most Popular
</a>

I would like to make my links clickable.

英文:

I am using react v17 and react-router-dom v6 in my project and my Route component use element instead of component. My problem begin when I try to get and show data belong the server and I use useffect hook.

<Routes>
  <Route exact path="/" element={<HomeFour />} />
  <Route path="/articles/:page" element={<Article />} exact />
<Routes>

Example of link

<a
  className="nav-link active"
  id="pills-home-tab"
  data-bs-toggle="pill"
  href="#pills-home"
  role="tab"
  aria-controls="pills-home" 
  aria-selected="true"
>
  Most Popular
</a>

I would like to see my links clickable.

答案1

得分: -1

以下是已翻译的内容:

似乎问题可能与您锚点(<a>)标签中的 href 属性相关。在 React Router v6 中,href 属性不用于应用程序内导航。相反,您应该使用由 react-router-dom 的 Link 组件提供的 to 属性。

为了解决问题,您可以将锚点标签替换为 Link 组件。这是一个示例:

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

// ...

<Link
  className="nav-link active"
  id="pills-home-tab"
  to="#pills-home"
  role="tab"
  aria-controls="pills-home"
  aria-selected="true"
>
  最受欢迎
</Link>

确保从 react-router-dom 中导入 Link 组件,然后用 Link 组件替换您的锚点标签(<a>)。通过这样做,您的链接应该可以点击,并正确处理 React 应用程序内的导航。

另外,如果您为 "/#pills-home" 定义了特定的路由,请确保在 Routes 组件中包括它,以确保在单击链接时呈现相应的组件。

英文:

It seems like the issue might be related to the href attribute in your anchor (<a>) tags. In React Router v6, the href attribute is not used for navigation within the application. Instead, you should use the to attribute provided by the Link component from react-router-dom.

To fix the issue, you can replace your anchor tags with the Link component. Here's an example:

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

// ...

&lt;Link
  className=&quot;nav-link active&quot;
  id=&quot;pills-home-tab&quot;
  to=&quot;#pills-home&quot;
  role=&quot;tab&quot;
  aria-controls=&quot;pills-home&quot; 
  aria-selected=&quot;true&quot;
&gt;
  Most Popular
&lt;/Link&gt;

Make sure you have the Link component imported from react-router-dom, and then replace your anchor tags (<a>) with the Link component. By doing this, your links should be clickable and properly handle navigation within your React application.

Additionally, if you have a specific route defined for "/#pills-home", make sure to include it in your Routes component to ensure that the corresponding component is rendered when the link is clicked.

huangapple
  • 本文由 发表于 2023年5月11日 17:51:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226323.html
匿名

发表评论

匿名网友

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

确定