javascript 如何使用反应实现导航栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50166035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement navbar using react
提问by Edward
I am trying to learn Reacton my own but having trouble creating a simple navbar for my web page. I would like to place the navbar in the index.jsxfile so that it shows up at the top of the page; below is what I have in the index.jsxfile.
我正在尝试自己学习React,但是在为我的网页创建一个简单的导航栏时遇到了麻烦。我想将导航栏放在index.jsx文件中,以便它显示在页面顶部;下面是我在index.jsx文件中的内容。
import React from 'react'
import { render } from 'react-dom'
import App from './components/App';
const node = document.querySelector('#app')
render(<App />, node);
回答by theJuls
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.
假设我们只是使用 vanilla react,您首先需要定义导航栏中的内容。没有样式或其他元素来使它漂亮的所有额外的绒毛,导航栏实际上只是一个列表。
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.
现在...当然,您需要一个导航栏,而不仅仅是在您的站点周围浮动的一些列表。要实现这一点,您需要使用一些 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.
在您的情况下,您正在使用 react-bootstrap 来完成大部分的跑腿工作。你不必太担心造型或任何事情,因为它会为你照顾好。
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.
如果您在 react-bootstrap 文档中查看此处,它会为您提供现成的组件,因此您不必进行所有样式设置。
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 Navbarcomponent will contain the entire of your Navbar, it will be the wrapper around it. The NavbarHeaderis the principal part which will stay to the left of the navbar and usually have either your brand name or icon. Finally, Navis 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.
所以要分解这个,Navbar组件将包含整个导航栏,它将是它的包装器。这NavbarHeader是将留在导航栏左侧的主要部分,通常带有您的品牌名称或图标。最后,Nav是什么会有不同的页面。在这种情况下,您不必担心样式或任何事情,因为所有 react-bootstrap 组件都已经为您处理好了。

