Search code examples
javascriptreactjsreact-routerreact-bootstrapreact-router-bootstrap

React component gets rendered under react bootstrap Navbar


I'm new to react and I'm trying to implement a Navbar using React-bootstrap which will route to another page without refreshing the browser.

To stop the page from refreshing I'm using react-router-bootstrap's LinkContainer component.

When I click on the "Calculator" link in the Navbar, the calculator page gets rendered, but it is placed behind the Navbar as can be seen in the image below.

Renders component under the navbar

Do I have to use CSS to reposition the component manually under the navbar? I thought the Calculator component would render underneath the navbar due to the <Route> component.

Underneath is my code for those who feel like helping out. Thanks in advance!

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>

Solution

  • I can see a property of the component Navbar called fixedTop so I guess this gives the navbar position: fixed;. This is a css property and has nothing to be with react. When you give some element fixed position the element is positioned relative to the browser window. I would use something like a <separator /> component after the <navbar> component and set it as a div with the height of the navbar. Otherwise you will have to put margin-top equals to the Navbar component's height in every single Component you want to render.