简体   繁体   English

React:为什么我在 React 组件中的链接不像在纯 HTML 页面中那样工作?

[英]React: why my links that are inside React components are not working as used to work in pure HTML page?

Basically I'm trying to remake some simple web page that I have initially created with HTML and CSS to be working rather on React.基本上,我正在尝试重新制作一些简单的 web 页面,这些页面是我最初使用 HTML 和 CSS 创建的,以便在 React 上工作。 I managed to redo the page to correctly display when it was moved into React, however I don't really understand why the navigation links that I have on top do not take me to the corresponding section on the same page anymore as well as why the external links to the project sites also stopped working.我设法重做页面以在将其移入 React 时正确显示,但是我真的不明白为什么我顶部的导航链接不再将我带到同一页面上的相应部分以及为什么指向项目网站的外部链接也停止工作。

Here is the project link code:这是项目链接代码:

import React from "react";

export default function ProjectTile(props) {
return (
  <div className="project-tile" id={props.id}>
    <a href={props.href} target="_blank" id={props.link_id}>
      <img
        className="project_screenshot"
        src={props.img_src}
        alt={props.img_alt}
      />
      <p className="project_name">
        <span className="brackets">&lt;</span> {props.title}{" "}
        <span className="brackets">/&gt;</span>
      </p>
    </a>
  </div>
 );
}

All props are getting mapped and loaded from the array with corresponding data where each object looks like this:所有道具都从数组中映射和加载,其中每个 object 看起来像这样:

  {
    id: "tribute_page",
    link_id: "https://codepen.io/konstantinkrumin/full/PooYQbG",
    img_src: "https://i.imgur.com/ynRuzOQ.png",
    img_alt: "tribute_page_screenshot",
    title: "Tribute Page"
  }

The navigation links used are the following:使用的导航链接如下:

import React from "react";

export default function Navbar() {
  return (
    <nav id="navbar">
    <a className="nav-link" href="#welcome-section">
      About
    </a>
    <a className="nav-link" href="#projects">
      Projects
    </a>
    <a className="nav-link" href="#contact">
      Contact
    </a>
  </nav>
 );
}

And each section they refer to have an id corresponding to the href indicated above.他们引用的每个部分都有一个对应于上述 href 的 id。

Here if the link to this project on codesandbox如果在代码沙箱上链接到这个项目

PS Everything used to work correctly when it was on HTML. PS 当它在 HTML 上时,一切都可以正常工作。 Also the contact links that seem to be set in similar way as project links are working.此外,似乎以与项目链接类似的方式设置的联系链接正在工作。

It looks like there's no href prop.看起来没有href道具。 Sounds like what you want is something like听起来你想要的就像

href={`#${props.id}`}

which would evaluate to href="#tribute_page" in this example.在此示例中,这将评估为href="#tribute_page"

You Have to try that your page url become:您必须尝试将您的页面 url 变为:

https://op6gq.csb.app#welcome-section

Not:不是:

https://op6gq.csb.app/#welcome-section

please attend to that / in address bar!请注意/在地址栏中!

Here are two things I think I found out:以下是我发现的两件事:

In the ProjectTile.js file, replace href = {props.href} by href={props.link_id and now project opens in codepen.ProjectTile.js文件中,将href = {props.href}替换为href={props.link_id ,现在项目在 codepen 中打开。

About the jump link you have made in nav-bar, I think it's because of problem of codesandbox.关于您在导航栏中制作的跳转链接,我认为这是因为codesandbox的问题。

If you manage to make your url to https://op6gq.csb.app#projects instead of https://op6gq.csb.app/#projects .如果你设法让你的 url 变成https://op6gq.csb.app#projects而不是https://op6gq.csb.app/#projects That's gonna work.那会奏效的。

Or directly visiting https://op6gq.csb.app/#welcome-section jump link too works well.或者直接访问https://op6gq.csb.app/#welcome-section跳转链接也可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM