React NavLink not working (doesn't re-render the pages)











up vote
0
down vote

favorite












I wanted to put a different menu in my website, and I decided that NavBar would be a great pick, as it is part of the react library. I did everything successfully and integrated the menu into my website, but then I noticed that all of my links do not work (except the drop-down buttons).



Here is my code:



App.js



    import React, { Component } from 'react';
import './App.css';
import Header from './components/Header/Header.jsx';
import HeaderTop from './components/HeaderTop/HeaderTop.jsx';
import Home from './components/Home/Home.jsx';
import SecondPage from './components/SecondPage/SecondPage.jsx';
import {BrowserRouter, Route, Switch}from 'react-router-dom';
import Footer from './components/Footer/Footer.jsx';
import Menu from './components/Menu/Menu.jsx';

class App extends Component {
render() {
return (

<div>
<div>
<HeaderTop />
<Menu />
</div>
<BrowserRouter>
<Switch>
<Route path="/" component={Home} exact/>
<Route path="/SecondPage" component={SecondPage} exact/>
</Switch>
</BrowserRouter>
<div>

</div>
</div>




);
}
}

export default App;


Menu.jsx



import React, { Component } from 'react';
import { Navbar, NavbarBrand, NavbarNav, NavbarToggler, Collapse, NavItem, NavLink, Dropdown, DropdownToggle, DropdownMenu, DropdownItem } from 'mdbreact';
import { BrowserRouter as Router } from 'react-router-dom';

class Menu extends Component {
constructor(props) {
super(props);
this.state = {
collapse: false,
isWideEnough: false,
dropdownOpen: false
};
this.onClick = this.onClick.bind(this);
this.toggle = this.toggle.bind(this);
}

onClick(){
this.setState({
collapse: !this.state.collapse,
});
}

toggle() {
this.setState({
dropdownOpen: !this.state.dropdownOpen
});
}

render() {
return (
<Router>
<Navbar color="indigo" dark expand="md" scrolling>
<NavbarBrand href="/">
<strong>HOME</strong>
</NavbarBrand>
{ !this.state.isWideEnough && <NavbarToggler onClick = { this.onClick } />}
<Collapse isOpen = { this.state.collapse } navbar>
<NavbarNav left>
<NavItem active>
<NavLink to="/">Home</NavLink>
</NavItem>
<NavItem>
<NavLink to="/SecondPage">Features</NavLink>
</NavItem>
<NavItem>
<NavLink to="#">Pricing</NavLink>
</NavItem>
<NavItem>
<Dropdown isOpen={this.state.dropdownOpen} toggle={this.toggle}>
<DropdownToggle nav caret>Dropdown</DropdownToggle>
<DropdownMenu>
<DropdownItem href="/SecondPage">Action</DropdownItem>
<DropdownItem href="#">Another Action</DropdownItem>
<DropdownItem href="#">Something else here</DropdownItem>
<DropdownItem href="#">Something else here</DropdownItem>
</DropdownMenu>
</Dropdown>
</NavItem>
</NavbarNav>
<NavbarNav right>
<NavItem>
<form className="form-inline md-form mt-0">
<input className="form-control mr-sm-2 mb-0 text-white" type="text" placeholder="Search" aria-label="Search"/>
</form>
</NavItem>
</NavbarNav>
</Collapse>
</Navbar>
</Router>
);
}
}
export default Menu;


The routes I am using are '/' and 'SecondPage' for testing purposes. If I change it to it will work, but won't work for the menu. I am rather new to React, so any help will be appreciated! Thanks in advance!










share|improve this question


























    up vote
    0
    down vote

    favorite












    I wanted to put a different menu in my website, and I decided that NavBar would be a great pick, as it is part of the react library. I did everything successfully and integrated the menu into my website, but then I noticed that all of my links do not work (except the drop-down buttons).



    Here is my code:



    App.js



        import React, { Component } from 'react';
    import './App.css';
    import Header from './components/Header/Header.jsx';
    import HeaderTop from './components/HeaderTop/HeaderTop.jsx';
    import Home from './components/Home/Home.jsx';
    import SecondPage from './components/SecondPage/SecondPage.jsx';
    import {BrowserRouter, Route, Switch}from 'react-router-dom';
    import Footer from './components/Footer/Footer.jsx';
    import Menu from './components/Menu/Menu.jsx';

    class App extends Component {
    render() {
    return (

    <div>
    <div>
    <HeaderTop />
    <Menu />
    </div>
    <BrowserRouter>
    <Switch>
    <Route path="/" component={Home} exact/>
    <Route path="/SecondPage" component={SecondPage} exact/>
    </Switch>
    </BrowserRouter>
    <div>

    </div>
    </div>




    );
    }
    }

    export default App;


    Menu.jsx



    import React, { Component } from 'react';
    import { Navbar, NavbarBrand, NavbarNav, NavbarToggler, Collapse, NavItem, NavLink, Dropdown, DropdownToggle, DropdownMenu, DropdownItem } from 'mdbreact';
    import { BrowserRouter as Router } from 'react-router-dom';

    class Menu extends Component {
    constructor(props) {
    super(props);
    this.state = {
    collapse: false,
    isWideEnough: false,
    dropdownOpen: false
    };
    this.onClick = this.onClick.bind(this);
    this.toggle = this.toggle.bind(this);
    }

    onClick(){
    this.setState({
    collapse: !this.state.collapse,
    });
    }

    toggle() {
    this.setState({
    dropdownOpen: !this.state.dropdownOpen
    });
    }

    render() {
    return (
    <Router>
    <Navbar color="indigo" dark expand="md" scrolling>
    <NavbarBrand href="/">
    <strong>HOME</strong>
    </NavbarBrand>
    { !this.state.isWideEnough && <NavbarToggler onClick = { this.onClick } />}
    <Collapse isOpen = { this.state.collapse } navbar>
    <NavbarNav left>
    <NavItem active>
    <NavLink to="/">Home</NavLink>
    </NavItem>
    <NavItem>
    <NavLink to="/SecondPage">Features</NavLink>
    </NavItem>
    <NavItem>
    <NavLink to="#">Pricing</NavLink>
    </NavItem>
    <NavItem>
    <Dropdown isOpen={this.state.dropdownOpen} toggle={this.toggle}>
    <DropdownToggle nav caret>Dropdown</DropdownToggle>
    <DropdownMenu>
    <DropdownItem href="/SecondPage">Action</DropdownItem>
    <DropdownItem href="#">Another Action</DropdownItem>
    <DropdownItem href="#">Something else here</DropdownItem>
    <DropdownItem href="#">Something else here</DropdownItem>
    </DropdownMenu>
    </Dropdown>
    </NavItem>
    </NavbarNav>
    <NavbarNav right>
    <NavItem>
    <form className="form-inline md-form mt-0">
    <input className="form-control mr-sm-2 mb-0 text-white" type="text" placeholder="Search" aria-label="Search"/>
    </form>
    </NavItem>
    </NavbarNav>
    </Collapse>
    </Navbar>
    </Router>
    );
    }
    }
    export default Menu;


    The routes I am using are '/' and 'SecondPage' for testing purposes. If I change it to it will work, but won't work for the menu. I am rather new to React, so any help will be appreciated! Thanks in advance!










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I wanted to put a different menu in my website, and I decided that NavBar would be a great pick, as it is part of the react library. I did everything successfully and integrated the menu into my website, but then I noticed that all of my links do not work (except the drop-down buttons).



      Here is my code:



      App.js



          import React, { Component } from 'react';
      import './App.css';
      import Header from './components/Header/Header.jsx';
      import HeaderTop from './components/HeaderTop/HeaderTop.jsx';
      import Home from './components/Home/Home.jsx';
      import SecondPage from './components/SecondPage/SecondPage.jsx';
      import {BrowserRouter, Route, Switch}from 'react-router-dom';
      import Footer from './components/Footer/Footer.jsx';
      import Menu from './components/Menu/Menu.jsx';

      class App extends Component {
      render() {
      return (

      <div>
      <div>
      <HeaderTop />
      <Menu />
      </div>
      <BrowserRouter>
      <Switch>
      <Route path="/" component={Home} exact/>
      <Route path="/SecondPage" component={SecondPage} exact/>
      </Switch>
      </BrowserRouter>
      <div>

      </div>
      </div>




      );
      }
      }

      export default App;


      Menu.jsx



      import React, { Component } from 'react';
      import { Navbar, NavbarBrand, NavbarNav, NavbarToggler, Collapse, NavItem, NavLink, Dropdown, DropdownToggle, DropdownMenu, DropdownItem } from 'mdbreact';
      import { BrowserRouter as Router } from 'react-router-dom';

      class Menu extends Component {
      constructor(props) {
      super(props);
      this.state = {
      collapse: false,
      isWideEnough: false,
      dropdownOpen: false
      };
      this.onClick = this.onClick.bind(this);
      this.toggle = this.toggle.bind(this);
      }

      onClick(){
      this.setState({
      collapse: !this.state.collapse,
      });
      }

      toggle() {
      this.setState({
      dropdownOpen: !this.state.dropdownOpen
      });
      }

      render() {
      return (
      <Router>
      <Navbar color="indigo" dark expand="md" scrolling>
      <NavbarBrand href="/">
      <strong>HOME</strong>
      </NavbarBrand>
      { !this.state.isWideEnough && <NavbarToggler onClick = { this.onClick } />}
      <Collapse isOpen = { this.state.collapse } navbar>
      <NavbarNav left>
      <NavItem active>
      <NavLink to="/">Home</NavLink>
      </NavItem>
      <NavItem>
      <NavLink to="/SecondPage">Features</NavLink>
      </NavItem>
      <NavItem>
      <NavLink to="#">Pricing</NavLink>
      </NavItem>
      <NavItem>
      <Dropdown isOpen={this.state.dropdownOpen} toggle={this.toggle}>
      <DropdownToggle nav caret>Dropdown</DropdownToggle>
      <DropdownMenu>
      <DropdownItem href="/SecondPage">Action</DropdownItem>
      <DropdownItem href="#">Another Action</DropdownItem>
      <DropdownItem href="#">Something else here</DropdownItem>
      <DropdownItem href="#">Something else here</DropdownItem>
      </DropdownMenu>
      </Dropdown>
      </NavItem>
      </NavbarNav>
      <NavbarNav right>
      <NavItem>
      <form className="form-inline md-form mt-0">
      <input className="form-control mr-sm-2 mb-0 text-white" type="text" placeholder="Search" aria-label="Search"/>
      </form>
      </NavItem>
      </NavbarNav>
      </Collapse>
      </Navbar>
      </Router>
      );
      }
      }
      export default Menu;


      The routes I am using are '/' and 'SecondPage' for testing purposes. If I change it to it will work, but won't work for the menu. I am rather new to React, so any help will be appreciated! Thanks in advance!










      share|improve this question













      I wanted to put a different menu in my website, and I decided that NavBar would be a great pick, as it is part of the react library. I did everything successfully and integrated the menu into my website, but then I noticed that all of my links do not work (except the drop-down buttons).



      Here is my code:



      App.js



          import React, { Component } from 'react';
      import './App.css';
      import Header from './components/Header/Header.jsx';
      import HeaderTop from './components/HeaderTop/HeaderTop.jsx';
      import Home from './components/Home/Home.jsx';
      import SecondPage from './components/SecondPage/SecondPage.jsx';
      import {BrowserRouter, Route, Switch}from 'react-router-dom';
      import Footer from './components/Footer/Footer.jsx';
      import Menu from './components/Menu/Menu.jsx';

      class App extends Component {
      render() {
      return (

      <div>
      <div>
      <HeaderTop />
      <Menu />
      </div>
      <BrowserRouter>
      <Switch>
      <Route path="/" component={Home} exact/>
      <Route path="/SecondPage" component={SecondPage} exact/>
      </Switch>
      </BrowserRouter>
      <div>

      </div>
      </div>




      );
      }
      }

      export default App;


      Menu.jsx



      import React, { Component } from 'react';
      import { Navbar, NavbarBrand, NavbarNav, NavbarToggler, Collapse, NavItem, NavLink, Dropdown, DropdownToggle, DropdownMenu, DropdownItem } from 'mdbreact';
      import { BrowserRouter as Router } from 'react-router-dom';

      class Menu extends Component {
      constructor(props) {
      super(props);
      this.state = {
      collapse: false,
      isWideEnough: false,
      dropdownOpen: false
      };
      this.onClick = this.onClick.bind(this);
      this.toggle = this.toggle.bind(this);
      }

      onClick(){
      this.setState({
      collapse: !this.state.collapse,
      });
      }

      toggle() {
      this.setState({
      dropdownOpen: !this.state.dropdownOpen
      });
      }

      render() {
      return (
      <Router>
      <Navbar color="indigo" dark expand="md" scrolling>
      <NavbarBrand href="/">
      <strong>HOME</strong>
      </NavbarBrand>
      { !this.state.isWideEnough && <NavbarToggler onClick = { this.onClick } />}
      <Collapse isOpen = { this.state.collapse } navbar>
      <NavbarNav left>
      <NavItem active>
      <NavLink to="/">Home</NavLink>
      </NavItem>
      <NavItem>
      <NavLink to="/SecondPage">Features</NavLink>
      </NavItem>
      <NavItem>
      <NavLink to="#">Pricing</NavLink>
      </NavItem>
      <NavItem>
      <Dropdown isOpen={this.state.dropdownOpen} toggle={this.toggle}>
      <DropdownToggle nav caret>Dropdown</DropdownToggle>
      <DropdownMenu>
      <DropdownItem href="/SecondPage">Action</DropdownItem>
      <DropdownItem href="#">Another Action</DropdownItem>
      <DropdownItem href="#">Something else here</DropdownItem>
      <DropdownItem href="#">Something else here</DropdownItem>
      </DropdownMenu>
      </Dropdown>
      </NavItem>
      </NavbarNav>
      <NavbarNav right>
      <NavItem>
      <form className="form-inline md-form mt-0">
      <input className="form-control mr-sm-2 mb-0 text-white" type="text" placeholder="Search" aria-label="Search"/>
      </form>
      </NavItem>
      </NavbarNav>
      </Collapse>
      </Navbar>
      </Router>
      );
      }
      }
      export default Menu;


      The routes I am using are '/' and 'SecondPage' for testing purposes. If I change it to it will work, but won't work for the menu. I am rather new to React, so any help will be appreciated! Thanks in advance!







      node.js reactjs routes react-router navbar






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 12:25









      Gergan Zhekov

      134




      134





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248743%2freact-navlink-not-working-doesnt-re-render-the-pages%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248743%2freact-navlink-not-working-doesnt-re-render-the-pages%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues