简体   繁体   English

我如何在 react-bootstrap 中从组件 header 到组件 App 中的另一个组件做一个 href?

[英]how do i do a href in react-bootstrap from the component header to another component in the component App?

What im trying to do is a link between different components in my page, i have a component header with a bunch of links and i want to make reference to anotother component on the same page, in other words im trying to do the following thing but with react:我想要做的是我页面中不同组件之间的链接,我有一个带有一堆链接的组件 header,我想引用同一页面上的另一个组件,换句话说,我试图做以下事情但是反应:

<a href="#move">Hi!</a>
<div id="#move">You have been moved</div> 

This is what i have in the header component im using react-bootstrap, i've found some ways to do it without it but i need to do it with it这就是我在使用 react-bootstrap 的 header 组件中所拥有的,我找到了一些没有它的方法,但我需要用它来做

export default function Header() {

    return (
        <>
            <Navbar variant="dark" expand="lg" style={{ "background-color": "#1A2A4E" }} sticky="top" >
                <Navbar.Brand href="#home">Logo</Navbar.Brand>
                <Navbar.Toggle aria-controls="basic-navbar-nav" />
                <Navbar.Collapse id="basic-navbar-nav">
                    <Nav className="mr-auto" />
                    <Nav>
                        <LinkContainer to={{
                            hash:"Footer"
                        }}>
                            <Nav.Link href="#">Actividades</Nav.Link>
                        </LinkContainer>
                        <Nav.Link href="#">Fechas</Nav.Link>
                        <Nav.Link href="#">Cursos</Nav.Link>
                        <Nav.Link href="#">Instalaciones</Nav.Link>
                        <Nav.Link href="#">Contacto</Nav.Link>

                        
                    </Nav>
                </Navbar.Collapse>
            </Navbar>
        </>
    )
}

and then in the App the first link gets the <a href="/#Footer" data-rb-event-key="/#Footer" class="active nav-link active">Actividades</a> but the href doesnt work the App component is:然后在应用程序中,第一个链接获取<a href="/#Footer" data-rb-event-key="/#Footer" class="active nav-link active">Actividades</a>但 href不起作用的 App 组件是:

function App() {
  return (
    <div className="App">
      <div className="imagen-fondo" />
      <div className="fondo-blaquesino" />
      <div className="">
        <Header Footer={<Footer/>} />
        <Infoinicio />
      </div>


      <div className="">
        <Actividades />
        <Cursos />
        <InscrCalendario />
        <Instalaciones />
        <Contacto />
        <div id="#Footer" />
        <Footer />
      </div>

    </div>
  );
}

So, you want to auto scroll to the selected id, rite?所以,你想自动滚动到选定的 id 吗? I achieve it using <NavLink /> from reactstrap .我使用 reactstrap 中的<NavLink />实现它。 Here is how it works下面是它的工作原理

NavigationBar.tsx导航栏.tsx

import React, { useState } from 'react';
import { withRouter } from 'react-router-dom';
import {
  Navbar, NavbarToggler, Collapse,
  Nav, NavItem, NavLink,
} from 'reactstrap';
import { HistoryProps } from '../../interfaces';
import './TopBar.scss';

function NavigationBar(props: HistoryProps) {
  const [isOpen, setIsOpen] = useState(false);

  const toggleNav = () => setIsOpen(!isOpen);

  const path = props.history.location.hash;

  return (
    <Navbar expand="md" className="navbar-wrapper">
      <NavbarToggler
        onClick={toggleNav}
        data-testid="navbar-toggler"
        className="navbar navbar-light outline-none"
      />
      <Collapse isOpen={isOpen} navbar data-testid="navbar-collapse" className="h-100">
        <Nav className="ml-auto h-100" navbar>
          <NavItem className="navbar-navitem pl-2 pr-2" active={!path || path.includes('#about-me')}>
            <NavLink className="font-size-12" href="#about-me">
              About Me
            </NavLink>
          </NavItem>
        </Nav>
      </Collapse>
    </Navbar>
  );
}

export default withRouter(NavigationBar);

And defined the target component/element, in my case, in another component.并在我的情况下在另一个组件中定义了目标组件/元素。

LandingPage.tsx登陆页面.tsx

import React from 'react';
import { Row } from 'reactstrap';
import './LandingPage.scss';

export default function LandingPage() {
  return (
    <div className="container-fluid">
      <Row className="about-me-wrapper id-wrapper" id="about-me">
        {/* Element here */}
      </Row>
    </div>
  );
}

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

相关问题 React react-bootstrap - 如何从外部组件关闭模态 window - React react-bootstrap - How do I close a modal window from an outside component 我怎样才能实现该 react-bootstrap 组件的 100% 可重用性? - How can i achieve 100% reusability of that react-bootstrap component? React.js如何在另一个组件中使用一个组件? - React.js How do I use a component in another component? react-bootstrap组件文档 - react-bootstrap component documentation 我如何从另一个 React 组件访问 redux 状态? - How do i access redux state from another react component? 如何在 Angular 中将数据从一个组件发送到另一个组件? - How do I send data from a Component to another Component in Angular? 如何在本机反应中从不同组件更改另一个组件的 state? - How do I change state of another component from different component in react native? 如何在另一个React组件上调用另一个函数? - How do I invoke another function on another react component? 如何将带有子项的 react-bootstrap 组件导入 kotlin-js react app - How to import react-bootstrap component with children into kotlin-js react app 从react-bootstrap导入任何组件都会引发错误 - importing any component from react-bootstrap throws error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM