最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to implement navbar using react - Stack Overflow

programmeradmin6浏览0评论

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);

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);
Share Improve this question edited Mar 26, 2020 at 5:33 Mario Boss 1,9753 gold badges23 silver badges45 bronze badges asked May 4, 2018 at 1:50 EdwardEdward 711 gold badge1 silver badge3 bronze badges 3
  • Are you using just react or any other library such as react-bootstrap or semantic-ui-react? – theJuls Commented May 4, 2018 at 1:53
  • yes I am using react-bootstrap – Edward Commented May 4, 2018 at 1:59
  • Try also to look at reactstrap. I found it much easier to use than react bootstrap. – Yossi Commented May 4, 2018 at 2:59
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论