7

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);
3
  • Are you using just react or any other library such as react-bootstrap or semantic-ui-react? Commented May 4, 2018 at 1:53
  • yes I am using react-bootstrap Commented May 4, 2018 at 1:59
  • Try also to look at reactstrap. I found it much easier to use than react bootstrap. Commented May 4, 2018 at 2:59

1 Answer 1

18

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.

Sign up to request clarification or add additional context in comments.

7 Comments

What if I wanted to make the navbar content and event handler dynamic?
@kyw what do you mean? Like read it from an array and render accordingly?
Thanks for the response. Well, like say my navbar has a back button. The back button is initially bounded to a click event handler that go back in history when clicked. But what if at a certain screen, I want the back button to behave differently, for example, when it's clicked, now it pops up alert. I mean the behaviour of my navbar is dynamic during runtime.
No problem. There are a couple ways of doing this. It also really depends on the whole structure of your app. If the parent component of your navbar changes, you can always pass down the functionality you want your button to have through props, then just use that on your click handler. If your case is a bit more similar to the example shown here, where the rendered navbar stays the same even when pages change, you could use the app's state to drive what function should be fired off depending on the state of your app or component.
Ideally I need to see your code surrounding the navbar to give you a proper answer. Do you have a thread opened relating to this? If not, you should open one with pieces of how your code currently looks so I can get a better picture of what's currently doing on.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.