admin管理员组文章数量:1207145
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
1 Answer
Reset to default 18Assuming 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.
本文标签: javascriptHow to implement navbar using reactStack Overflow
版权声明:本文标题:javascript - How to implement navbar using react - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738734564a2109522.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论