Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement navbar using react

I am trying to learn React on my own but having trouble creating a simple navbar for my web page. I would like to place the navbar in the index.jsx file so that it shows up at the top of the page; below is what I have in the index.jsx file.

import React from 'react'
import { render } from 'react-dom'
import App from './components/App';

const node = document.querySelector('#app')

render(<App />, node);
like image 705
Edward Avatar asked May 04 '18 01:05

Edward


Video Answer


1 Answers

Assuming we are just with vanilla react, you first need to define what is in your navbar. Without all the extra fluff that comes with styling or other elements to make it pretty, the navbar really is just a list.

So with that being said, your navbar component will look like this:

class Navbar extends React.Component{
    render() {
        return (
            <div>
              <ul id="nav">
                <li><a href="#">Home</a></li>
                <li><a href="#">About</a></li>
                <li><a href="#">FAQ</a></li>
                <li><a href="#">Contact</a></li>
              </ul>
            </div>
        );
    }
}

Now... You want a navbar of course, not just some list floating around your site. To achieve this, you will need to play around with some CSS.

First, make it horizontal:

#nav {
    width: 100%;
    float: left;
    margin: 0 0 3em 0;
    padding: 0;
    list-style: none; }
#nav li {
    float: left; }

Now space the elements out and decorate them a bit:

#nav {
    width: 100%;
    float: left;
    margin: 0 0 3em 0;
    padding: 0;
    list-style: none;
    background-color: #f2f2f2;
    border-bottom: 1px solid #ccc; 
    border-top: 1px solid #ccc; }

#nav li a {
        display: block;
        padding: 8px 15px;
        text-decoration: none;
        font-weight: bold;
        color: #069;
        border-right: 1px solid #ccc; }

Source of the CSS. As well as further explanations to what is being done

Here is the app that will result in this:

class Navbar extends React.Component{
    render() {
        return (
            <div>
              <ul id="nav">
                <li><a href="#">Home</a></li>
                <li><a href="#">About</a></li>
                <li><a href="#">FAQ</a></li>
                <li><a href="#">Contact</a></li>
              </ul>
            </div>
        );
    }
}

class App extends React.Component {
  render () {
    return (
      <div>
        <Navbar/>
        <div>
          [Page content here]
        </div>
      </div>
    )
  }
}



ReactDOM.render(
    <App />,
    document.getElementById('app')
);
#nav {
	width: 100%;
	float: left;
	margin: 0 0 3em 0;
	padding: 0;
	list-style: none; }
#nav li {
	float: left; }

#nav li a {
		display: block;
		padding: 8px 15px;
		text-decoration: none;
		font-weight: bold;
		color: #069;
		border-right: 1px solid #ccc; }

#nav li a {
		display: block;
		padding: 8px 15px;
		text-decoration: none;
		font-weight: bold;
		color: #069;
		border-right: 1px solid #ccc; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"></div>

In your case, you are using react-bootstrap which gets the bulk of the legwork done. You won't have to worry much about styling or anything because it will take care of that for you.

If you look here in the react-bootstrap docs, it gives you ready made components so that you don't have to do all that styling.

class MyNavbar extends React.Component{
    render() {
        return (
           <Navbar>
            <Navbar.Header>
              <Navbar.Brand>
                <a href="#home">My Brand</a>
              </Navbar.Brand>
            </Navbar.Header>
            <Nav>
              <NavItem href="#">
                Home
              </NavItem>
              <NavItem href="#">
                About
              </NavItem>
              <NavItem href="#">
                FAQ
              </NavItem>
              <NavItem href="#">
                Contact Us
              </NavItem>
            </Nav>
          </Navbar>
        );
    }
}

So to break this down, the Navbar component will contain the entire of your Navbar, it will be the wrapper around it. The NavbarHeader is the principal part which will stay to the left of the navbar and usually have either your brand name or icon. Finally, Nav is what will have the different pages. In this case you don't have to worry about the styling or anything because all of the react-bootstrap components have already taken care of that for you.

like image 134
theJuls Avatar answered Sep 30 '22 10:09

theJuls