1

i'm just new to react router, this is my first try :)

Problem

If i click on the proper link ( <NavLink to='/BubbleSort'>Bubble sort</NavLink> e.g.) the i don't get the content, the page still continue to be without content.

Code

import React, { Component } from 'react';
import './App.css';
import {
  Route,
  HashRouter
} from 'react-router-dom';
import BubbleSort from './Components/BubbleSort';
import InsertionSort from './Components/InsertionSort';
import Menu from './Components/Menu';


class App extends Component {

  render() {
    return (
    <HashRouter>
      <div className="App">
        <Menu/>
        <div className="content">
          <Route path="./Components/BubbleSort" component = { BubbleSort }/>
          <Route path="./Components/InsertionSort" component = { InsertionSort }/>
        </div>
      </div>
    </HashRouter>
    );
  }
}

export default App;

Down below we have the Menu component

import React, {Component} from 'react';
import './Menu.css';
import {
    NavLink,
  } from 'react-router-dom';



class Menu extends Component {

    render() {
        return(
                <nav className="navbar">
                    <div 
                        className="nav-button" >
                        <NavLink to='/BubbleSort'>Bubble sort</NavLink>
                    </div>
                    <div 
                        className="nav-button">
                        <NavLink to="./InserionSort">Insertion sort</NavLink>
                    </div>
                    <div 
                        className="nav-button" >
                        <NavLink>Selection sort</NavLink>
                    </div>
                    <div 
                        className="nav-button" 
                        a href="/mergeSort">
                        Merge sort
                    </div>
                    <div 
                        className="nav-button" 
                        a href="/quickSort">
                        Quick sort
                    </div>
                    <div 
                        className="nav-button" 
                        a href="/radixSort">
                        Radix sort
                    </div>
                </nav>
        )
    }
}

export default Menu;

Expected Behaviour

if I click on <NavLink to='/BubbleSort'>Bubble sort</NavLink>the page has to shows the actual content of the BubbleSort component.

Consideration

1) The Menu component in the folder "./Components/Menu", the App.js is in the main Src folder, the BubbleSort.js is in the folder "./Components/BubbleSort"

i followed this link to made this little navbar, i think that the problem is something related to the use of the Menu component, but i don't know how to fix it.

5
  • try add ´<Switch></Switch>´ inside <Menu> and out of <div class="content"> Commented Mar 20, 2019 at 14:59
  • Route and NavLink should have relative path Commented Mar 20, 2019 at 15:00
  • @JorgeFélixCazarez The problem here is because of improper syntax and component usage. Switch is a totally different story. :) Commented Mar 20, 2019 at 15:02
  • @Legeo is using different path and to in Route and NavLink components respectively. That' s why the NavLink are not working Commented Mar 20, 2019 at 15:04
  • I didn't see the <NavLink>, its true @AjayGupta Commented Mar 20, 2019 at 15:05

2 Answers 2

2

Route path should not be the component file path. it should be what you put inside (to) props inside NavLink. Demo Reference code pen

import React, { Component } from 'react';
import './App.css';
import {
  Route,
  HashRouter
} from 'react-router-dom';
import BubbleSort from './Components/BubbleSort';
import InsertionSort from './Components/InsertionSort';
import Menu from './Components/Menu';


class App extends Component {

  render() {
    return (
    <HashRouter>
      <div className="App">
        <Menu/>
        <div className="content">
          <Route path="/BubbleSort" component = { BubbleSort }/>
          <Route path="/InsertionSort" component = { InsertionSort }/>
        </div>
      </div>
    </HashRouter>
    );
  }
}

export default App;

import React, {Component} from 'react';
import './Menu.css';
import {
    NavLink,
  } from 'react-router-dom';



class Menu extends Component {

    render() {
        return(
                <nav className="navbar">
                    <div 
                        className="nav-button" >
                        <NavLink to='/BubbleSort'>Bubble sort</NavLink>
                    </div>
                    <div 
                        className="nav-button">
                        <NavLink to="/InserionSort">Insertion sort</NavLink>
                    </div>
                    <div 
                        className="nav-button" >
                        <NavLink>Selection sort</NavLink>
                    </div>
                    <div 
                        className="nav-button" 
                        a href="/mergeSort">
                        Merge sort
                    </div>
                    <div 
                        className="nav-button" 
                        a href="/quickSort">
                        Quick sort
                    </div>
                    <div 
                        className="nav-button" 
                        a href="/radixSort">
                        Radix sort
                    </div>
                </nav>
        )
    }
}

export default Menu;

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your contribution. Please make sure when you answer a question, you also explain what was the problem with the OP's code. It helps to better understand the problem and the solution. Thanks! :)
2

Route and NavLink Component should be written like this:-

<Route path='/Components/BubbleSort' component={ BubbleSort } />
<Route path='/Components/InsertionSort' component={ InsertionSort } />

NavLink component will be written like the following:-

<NavLink to='/Components/InsertionSort'>Insertion Sort</NavLink>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.