简体   繁体   English

如何在 React 和新页面中链接 2 个组件仍然有导航栏

[英]How to link 2 components in React and in a new page still have a navigation bar

I have built a website to React.我已经建立了一个网站来响应。 My code is here https://codesandbox.io/s/3d-tool-zjm5m?file=/src/components/modeling.jsx .我的代码在这里https://codesandbox.io/s/3d-tool-zjm5m?file=/src/components/modeling.jsx What I want is when the user clicks on the button "Go upload" in Modeling tab, it will link to a new page of upload.jsx with still have the navigation bar.我想要的是当用户点击 Modeling 选项卡中的“Go upload”按钮时,它会链接到一个新的 upload.jsx 页面,并且仍然有导航栏。

App.js code: App.js 代码:

import React, { Component } from 'react'
import Navigation from './components/navigation';
import Header from './components/header';
import About from './components/about';
import Modeling from './components/modeling';
import Resources from './components/resources';
import Team from './components/Team';
import JsonData from './data/data.json';

export class App extends Component {
  state = {
    landingPageData: {},
  }
  getlandingPageData() {
    this.setState({landingPageData : JsonData})
  }

  componentDidMount() {
    this.getlandingPageData();
  }

  render() {
    return (
      <div>
        <Navigation />
        <Header data={this.state.landingPageData.Header} />
        <About data={this.state.landingPageData.About} />
        <Modeling data={this.state.landingPageData.Modeling} />
        <Resources data={this.state.landingPageData.Resources} /> 
        <Team data={this.state.landingPageData.Team} />
    
      </div>
    )
  }
}

export default App;

Modeling.jsx code: Modeling.jsx 代码:

import React, { Component } from "react";
import Upload from "./upload";

export class Modeling extends Component {
  uploadButton = (event) => {
    event.preventDefault();
    this.props.history.push({
      pathname: "/upload"
    });
  };
  render() {
    return (
      <div id="modeling">
        <div className="container">
          <div className="row">
            <div className="about-text">
              <h2>3D MODELING</h2>
            </div>
          </div>
          <div className=" wow fadeInUp slow">
            <div className="row pt-5 justify-content-center">
              <div className="col text-center">
                <h1>
                  <b>Pick what's right for you </b>
                </h1>
              </div>
            </div>
          </div>
          <div class="row p-5 p-md-0 pt-md-5 justify-content-around">
            <div
              class="col-md-5 mb-4 m-md-0 ml-md-5 modern-card card card-body shadow text-center wow fadeIn slow"
              data-wow-delay="0.2s"
            >
              <h2 class="mb-3 blue">
                <b>A 3D model from a 2D image.</b>
              </h2>

              <button
                type="button"
                class="btn btn-primary go-button"
                onclick={this.uploadButton}
              >
                Go upload
              </button>
            </div>

            <div
              class="col-md-5 mr-md-5 modern-card card card-body shadow text-center wow fadeIn slow"
              data-wow-delay="0.4s"
            >
              <h2 class="mb-3 blue">
                <b>A virtual tour from a 3D model.</b>
              </h2>

              <button
                type="button"
                class="btn btn-primary go-button"
                
              >
                Go!
              </button>
            </div>
          </div>
        </div>
      </div>
    );
  }
}

export default Modeling;

Can someone stop by give me any suggestions? I have really appreciated it. Thanks!

Your navigation bar has hashlinks to sections on what amounts to a "homepage".您的导航栏具有指向“主页”部分的哈希链接。 When you introduce app navigation and want more than a single page then you should use Link components versus anchor tags.当您引入应用导航并且想要多个页面时,您应该使用Link组件而不是锚标记。 You also need to place the Navigation component on a route so it has access to the Router context.您还需要将Navigation组件放置在路由上,以便它可以访问Router上下文。

App.js应用程序.js

Since you already wrap the entire app in a Router you need only place the Navigation and "homepage" components in Route s.由于您已经将整个应用程序包装在Router您只需将Navigation和“主页”组件放在Route Render the "homepage" components into an anonymous component and remember to pass the route props through to each component.将“主页”组件渲染为匿名组件,并记住将路由道具传递给每个组件。

<div>
  <Route component={Navigation} />
  <Switch>
    <Route path="/upload" component={Upload} />
    <Route
      render={(routeProps) => (
        <>
          <Header {...routeProps} data={this.state.landingPageData.Header} />
          <About {...routeProps} data={this.state.landingPageData.About} />
          <Modeling {...routeProps} data={this.state.landingPageData.Modeling} />
          <Resources {...routeProps} data={this.state.landingPageData.Resources} />
          <Team {...routeProps} data={this.state.landingPageData.Team} />
        </>
      )}
    />
  </Switch>
</div>

Navigation.jsx导航.jsx

In order to keep the hashlink functionality working, and from other pages you will need to also import a HashLink component.为了保持 hashlink 功能正常工作,您还需要从其他页面导入HashLink组件。

import { HashLink } from "react-router-hash-link";

And change all the anchor tags to HashLink components.并将所有锚标签更改为HashLink组件。

<HashLink className="navbar-brand page-scroll" to="/#page-top">
  3D RECONSTRUCTION TOOL
</HashLink>{" "}

...

<HashLink to="/#page-top" className="page-scroll">
  Home
</HashLink>
// ...etc...

Modeling.jsx建模.jsx

Fix the button's onClick prop, it is onClick not onclick .修复按钮的onClick属性,它是onClick而不是onclick Because route props were passed in from App.js , the history prop is now available for the imperative navigation.因为路由道具是从App.js ,所以history道具现在可用于命令式导航。

uploadButton = (event) => {
  event.preventDefault();
  this.props.history.push({
    pathname: "/upload"
  });
};

...

<button
  type="button"
  className="btn btn-primary go-button"
  onClick={this.uploadButton}
>
  Go upload
</button>

Now you may notice when navigating to the new page on "/upload" that the navigation header is still fixed to the top of the window and the page is rendered underneath.现在您可能会注意到,当导航到“/upload”上的新页面时,导航标题仍然固定在窗口的顶部,而页面呈现在下方。 This is style being applied from react-bootstrap .这是从react-bootstrap应用的样式。 To resolve this you can add padding-top to the body to push content down under the header.要解决此问题,您可以向正文添加padding-top以将内容下推到标题下。

body {
  padding-top: /* whatever the navigation header height is */;
}

Your header is dynamic though, depending on view width, so you will likely need to check the view width and set this in JS.不过,您的标题是动态的,具体取决于视图宽度,因此您可能需要检查视图宽度并在 JS 中进行设置。 Here's a promising solution: https://stackoverflow.com/a/38105035/8690857这是一个很有前途的解决方案: https : //stackoverflow.com/a/38105035/8690857

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

相关问题 如何获得一个链接到新页面的按钮,并预先填充搜索栏 - How can I get a button to link to a new page, and have a search bar pre filled 如何通过单击导航栏(单站点页面)中的链接来关闭导航栏? - How can I close my Navigation Bar with a click on a link in that bar (One-Site-Page)? 如何使用反应导航导航到反应原生的新屏幕并且组件位于不同的文件中 - How to navigate to a new screen in react native using react navigation and the components are in a different file React Native Navigator:仅在某些组件上显示导航栏 - React Native Navigator : Show navigation bar on certain components only 页面导航后反应路由器链接仍然存在 - React Router Link Persists after Page Navigation 如何在导航栏中更改当前链接颜色 - How to change the current link color in navigation bar 如果组件代码有错误,仍然呈现页面 - React/Nextjs - Still render the page if the components code has error - React/Nextjs 如何在保持底部导航的同时在组件之间导航? - How to navigate between components while keeping bottom navigation still? 如何在 React 中使用导航切换组件? - How to switch components using navigation in React? 如何在固定的导航栏下显示图像? - How to have an image under a fixed navigation bar?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM