ReactJS Bootstrap导航栏和路由无法一起使用 [英] ReactJS Bootstrap Navbar and Routing not working together
问题描述
我正在尝试使用ReactJS创建一个简单的Webapp,我想使用React-Bootstrap提供的 Navbar
.
I am trying to create a simple Webapp using ReactJS, and I wanted to use the Navbar
provided by React-Bootstrap.
我创建了一个包含类 Navigation
的 Navigation.js
文件,以将 Navbar
和路由与 App.js分开
文件.但是,这两个部分似乎都不起作用.当我加载页面时,它只是空的,没有导航栏.谁能发现一个错误?
I created a Navigation.js
file containing a class Navigation
to separate the Navbar
and the Routing from the App.js
file. However, both parts do not seem to work. When I load the page, it is just empty, there is no Navbar. Can anyone spot a mistake?
Navigation.js:
Navigation.js:
import React, { Component } from 'react';
import { Navbar, Nav, Form, FormControl, Button, NavItem } from 'react-bootstrap';
import { Switch, Route } from 'react-router-dom';
import { Home } from './Page';
class Navigation extends Component {
render() {
return (
<div>
<div>
<Navbar>
<Navbar.Brand href="/">React-Bootstrap</Navbar.Brand>
<Navbar.Collapse>
<Nav className="mr-auto">
<NavItem eventkey={1} href="/">
<Nav.Link href="/">Home</Nav.Link>
</NavItem>
</Nav>
<Form inline>
<FormControl type="text" placeholder="Search" className="mr-sm-2" />
<Button variant="outline-success">Search</Button>
</Form>
</Navbar.Collapse>
</Navbar>
</div>
<div>
<Switch>
<Route exact path='/' component={Home} />
<Route render={function () {
return <p>Not found</p>
}} />
</Switch>
</div>
</div>
);
}
}
export default Navigation;
App.js:
import React, { Component } from 'react';
import Navigation from './components/routing/Navigation';
class App extends Component {
render() {
return (
<div id="App">
<Navigation />
</div>
);
}
}
export default App;
我尝试使用包含来自 react-router-bootstrap
的 LinkContainer
的 NavItem
,这导致了相同的结果.
I tried using a NavItem
containing a LinkContainer
from react-router-bootstrap
already, which led to the same result.
为了完整起见,Page.js:
Just for completeness, Page.js:
import React, { Component } from 'react';
import { Link } from 'react-router-dom';
export const Page = ({ title }) => (
<div className="App">
<div className="App-header">
<h2>{title}</h2>
</div>
<p className="App-intro">
This is the {title} page.
</p>
<p>
<Link to="/">Home</Link>
</p>
<p>
<Link to="/about">About</Link>
</p>
<p>
<Link to="/settings">Settings</Link>
</p>
</div>
);
export const About = (props) => (
<Page title="About"/>
);
export const Settings = (props) => (
<Page title="Settings"/>
);
export const Home = (props) => (
<Page title="Home"/>
);
推荐答案
首先,在您的代码段中,您似乎没有将代码包装在 Router
中,因此您应该确保您是在 App
内部或 ReactDOM.render
中进行此操作:
First of all, in your snippets it doesn't seem like you're wrapping your code in a Router
, so you should make sure that you're doing that inside App
or in ReactDOM.render
:
import { BrowserRouter } from 'react-router-dom';
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
rootElement
);
接下来,您的特定问题是您呈现的是React-Bootstrap的 Nav.Link
而不是react-router的 Link
组件,因此路由器无法拾取您的路线变更.幸运的是,react-bootstrap在其大多数组件中都提供了一个渲染道具,以指定您要渲染的组件或元素(如果您不希望使用默认组件).切换到这样的内容:
Next, your specific problem is that you're rendering react-bootstrap's Nav.Link
instead of react-router's Link
component, so the router is not picking up your route changes. Fortunately, react-bootstrap provides a render prop in most of its components to specify which component or element you want to render if you don't want the default. Switch to something like this:
import { Switch, Route, Link } from 'react-router-dom';
class Navigation extends Component {
render() {
return (
<div>
<div>
<Navbar>
<Navbar.Brand as={Link} to="/" >React-Bootstrap</Navbar.Brand>
<Navbar.Collapse>
<Nav className="mr-auto">
<NavItem eventkey={1} href="/">
<Nav.Link as={Link} to="/" >Home</Nav.Link>
</NavItem>
</Nav>
<Form inline>
<FormControl type="text" placeholder="Search" className="mr-sm-2" />
<Button variant="outline-success">Search</Button>
</Form>
</Navbar.Collapse>
</Navbar>
</div>
<div>
<Switch>
<Route exact path='/' component={Home} />
<Route render={function () {
return <p>Not found</p>
}} />
</Switch>
</div>
</div>
);
}
}
这篇关于ReactJS Bootstrap导航栏和路由无法一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!