在React Bootstrap Navbar下渲染React组件

时间:2018-10-18 18:31:30

标签: javascript reactjs react-router react-bootstrap react-router-bootstrap

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

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

当我单击导航栏中的“计算器”链接时,将渲染计算器页面,但是它位于导航栏的后面,如下图所示。

Renders component under the navbar

我是否必须使用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>

1 个答案:

答案 0 :(得分:1)

我可以看到名为Path spreadsheetsDir = Files.createTempDirectory(null); Path excel = null; while ((ze = zin.getNextEntry()) != null) { String name = ze.getName(); if (name.endsWith(".xlsx")) { excel = spreadsheetsDir.resolve(name)); Files.copy(zin, excel); } else if (name.endsWith(".svg")) { AnimationSvg animationSvg = new AnimationSvg(); animationSvg.setName(name); animationSvg.setSvgContent( new String(zin.readAllBytes(), StandardCharsets.UTF_8)); animationSvgs.add(animationSvg); } zin.closeEntry(); } 的组件Navbar的一个属性,因此我想这给出了导航栏fixedTop。这是一个CSS属性,与react没有任何关系。当您为某个元素提供固定位置时,该元素相对于浏览器窗口定位。我将在position: fixed;组件之后使用类似<separator />的组件,并将其设置为具有导航栏高度的div。否则,您必须在要渲染的每个组件中都将<navbar>等于Navbar组件的高度。