1

I want to send my user to the login page from the main page after they click login button. But the react router dom's Link is not working and it is not redirecting to the login page.

Having issue with :

 <Link to="/login">Login</Link>

The App.js code as well as the main page code are attached below. So please help me out.

App.js code :

import React, { Component } from 'react';
import './App.css';
import 'bootstrap/dist/css/bootstrap.min.css';
import { Provider } from 'react-redux';
import store from './store';
import AppNavbar from './components/appNavbar';
import Login from './components/login';
import { BrowserRouter, Switch, Route } from 'react-router-dom';
import FacultyMain from "./components/facultyMain";

class App extends Component{
    render() {
        return (
            <Provider store={store} >
                <AppNavbar />
                <BrowserRouter>
                    <Switch>
                        <Route path='/' component={FacultyMain} />
                        <Route path='/login' component={Login} />
                    </Switch>
                </BrowserRouter>
            </Provider>
        );
    }
}

export default App;

The page where the link is :

import React, { Component } from 'react';
import {
  Card, CardImg, CardText, CardBody,
  CardTitle, CardSubtitle, Button
} from 'reactstrap';
import image from '../logo.jpeg';
import { Link } from 'react-router-dom';

class FacultyMain extends Component {
    render() {
        return (
            <div>
                <Link to="/login">Login</Link>
            </div>
        );
    }
};

export default FacultyMain;

package.json :

{
  "name": "virtual-lab",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "axios": "^0.21.0",
    "bootstrap": "^4.5.3",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-redux": "^7.2.2",
    "react-router-dom": "^5.2.0",
    "react-scripts": "4.0.1",
    "reactstrap": "^8.7.1",
    "redux": "^4.0.5",
    "redux-thunk": "^2.3.0",
    "web-vitals": "^0.2.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "redux-devtools-extension": "^2.13.8"
  }
}

6
  • 2
    Im not sure, but try putting exact to the routes, like: <Route exact path=... Commented Dec 1, 2020 at 13:53
  • Does the URL change when you click the button? Commented Dec 1, 2020 at 13:53
  • Yes the url changes. Commented Dec 1, 2020 at 13:56
  • i think putting exact on each route may make a difference Commented Dec 1, 2020 at 13:58
  • Yea, since he has put the most general route at the top "/" it will always match without exact. @BhagyarajHaranale take a look at why exact is used, stackoverflow.com/a/57836415/13830075 Commented Dec 1, 2020 at 14:01

1 Answer 1

1

The problem was with the '/' path. Using exact solved it

<Route exact path='/' component={FacultyMain} />

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

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.