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

时间:2020-10-21 19:59:47

标签: javascript reactjs navigation components

我已经建立了一个React网站。我的代码在这里https://codesandbox.io/s/3d-tool-zjm5m?file=/src/components/modeling.jsx。我想要的是,当用户单击“建模”选项卡中的“开始上传”按钮时,它将链接到上载有导航栏的upload.jsx的新页面。

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代码:

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!

1 个答案:

答案 0 :(得分:1)

您的导航栏中包含指向“首页”部分的哈希链接。当您引入应用导航并且想要多个页面时,则应使用Link组件而不是锚标记。您还需要将Navigation组件放在路由上,以便它可以访问Router上下文。

App.js

由于您已经将 entire 应用程序包装在Router中,因此只需将Navigation和“主页”组件放在Route中。将“首页”组件呈现为匿名组件,并记住将路由道具传递给每个组件。

<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

为了保持哈希链接功能正常运行,还需要从其他页面导入HashLink组件。

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

并将所有锚标记更改为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

修正按钮的onClick属性,它是onClick而不是onclick。由于路线道具是从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>

现在,当您导航到“ / upload”上的新页面时,您可能会注意到导航标题仍固定在窗口顶部,并且页面呈现在下面。这是从react-bootstrap开始应用的样式。要解决此问题,您可以在正文中添加padding-top,以将内容下推到标题下方。

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

尽管标题是动态的,取决于视图的宽度,所以您可能需要检查视图的宽度并在JS中进行设置。这是一个很有前途的解决方案:https://stackoverflow.com/a/38105035/8690857