ReactJS Bootstrap Navbar and Routing not working together

后端 未结 4 449
借酒劲吻你
借酒劲吻你 2020-12-13 19:29

I am trying to create a simple Webapp using ReactJS, and I wanted to use the Navbar provided by React-Bootstrap.

I created a Navigation.js

4条回答
  •  無奈伤痛
    2020-12-13 19:54

    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(
      
        
      , 
      rootElement
      );
    

    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 (
          
    React-Bootstrap
    Not found

    }} />
    ); } }

提交回复
热议问题