React 组件在 React bootstrap Navbar 下呈现

React component gets rendered under react bootstrap Navbar

我是 React 新手,我正在尝试使用 React-bootstrap 实现导航栏,它将在不刷新浏览器的情况下路由到另一个页面。

为了阻止页面刷新,我使用了 react-router-bootstrap 的 LinkContainer 组件。

当我点击导航栏中的 "Calculator" link 时,计算器页面得到呈现,但它位于导航栏后面,如下图所示。

我是否必须使用 CSS 在导航栏下手动重新定位组件? 由于 <Route> 组件,我认为计算器组件会呈现在导航栏下方。

下面是我的代码,供那些想帮忙的人使用。提前致谢!

Index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './app/App';
import registerServiceWorker from './registerServiceWorker';

ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();

App.js

import React, { Component } from 'react';
import Navbar from '../Navbar/Navbar';
import { BrowserRouter as Router, Route } from 'react-router-dom';
import Calculator from '../Calculator/Calculator';
import './App.css';


class App extends Component {
  render() {
    return (
      <Router>
        <div className="App">
          <Navbar />
          <Route path="/Calculator" component={Calculator} />
        </div>
      </Router>
    );
  }
}

export default App;

Navbar.js

import React, { Component } from 'react';
import { Navbar, Nav, NavItem } from 'react-bootstrap/lib';
import { LinkContainer } from 'react-router-bootstrap';
import { Link } from 'react-router-dom';
import './Navbar.css';

class NavBar extends Component {

  render() {
    return (
      <div>
        <Navbar fixedTop collapseOnSelect>
          <Navbar.Header>
            <Navbar.Brand>
              <Link to="/">Home</Link>
            </Navbar.Brand>
            <Navbar.Toggle />
          </Navbar.Header>
          <Navbar.Collapse>
            <Nav>
              <LinkContainer to="/Calculator">
                <NavItem>Calculator</NavItem>
              </LinkContainer>
            </Nav>
          </Navbar.Collapse>
        </Navbar>        
      </div>
    );
  };
}

export default NavBar;

Calculator.js

import React, { Component } from 'react';

class Calculator extends Component {
  render() {
    return (
      <div>
        <p>Calculator page</p>
      </div>
    );
  }
}

export default Calculator;

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="theme-color" content="#000000">
    <!--
      manifest.json provides metadata used when your web app is added to the
      homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
    -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json">    
    <!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <title>Thea Guide</title>
  </head>
  <body>
    <noscript>
      You need to enable JavaScript to run this app.
    </noscript>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  </body>
</html>

我可以看到名为 fixedTop 的组件 Navbar 的 属性,所以我想这提供了导航栏 position: fixed;。这是一个 css 属性 并且与 React 无关。当你给一些元素固定位置时,元素相对于浏览器定位 window。我会在 <navbar> 组件之后使用类似 <separator /> 的组件,并将其设置为具有导航栏高度的 div。否则,您必须在要渲染的每个组件中将 margin-top 等于 Navbar 组件的高度。