Search code examples
javascriptreactjsreact-routerreact-router-dom

Uncaught runtime errors: useNavigate() may be used only in the context of a <Router> component


I have created a new react project. I am new to react, I am trying to navigate after clicking on the text but it is giving me this error.

Uncaught runtime errors:
×
ERROR
useNavigate() may be used only in the context of a <Router> component.
    at invariant (http://localhost:3000/static/js/bundle.js:1123:11)
    at useNavigateUnstable (http://localhost:3000/static/js/bundle.js:66522:102)
    at useNavigate (http://localhost:3000/static/js/bundle.js:66519:46)
    at App (http://localhost:3000/static/js/bundle.js:83:81)
    at renderWithHooks (http://localhost:3000/static/js/bundle.js:26618:22)
    at mountIndeterminateComponent (http://localhost:3000/static/js/bundle.js:29904:17)
    at beginWork (http://localhost:3000/static/js/bundle.js:31200:20)
    at HTMLUnknownElement.callCallback (http://localhost:3000/static/js/bundle.js:16210:18)
    at Object.invokeGuardedCallbackDev (http://localhost:3000/static/js/bundle.js:16254:20)
    at invokeGuardedCallback (http://localhost:3000/static/js/bundle.js:16311:35)
ERROR
useNavigate() may be used only in the context of a <Router> component.
    at invariant (http://localhost:3000/static/js/bundle.js:1123:11)
    at useNavigateUnstable (http://localhost:3000/static/js/bundle.js:66522:102)
    at useNavigate (http://localhost:3000/static/js/bundle.js:66519:46)
    at App (http://localhost:3000/static/js/bundle.js:83:81)
    at renderWithHooks (http://localhost:3000/static/js/bundle.js:26618:22)
    at mountIndeterminateComponent (http://localhost:3000/static/js/bundle.js:29904:17)
    at beginWork (http://localhost:3000/static/js/bundle.js:31200:20)
    at HTMLUnknownElement.callCallback (http://localhost:3000/static/js/bundle.js:16210:18)
    at Object.invokeGuardedCallbackDev (http://localhost:3000/static/js/bundle.js:16254:20)
    at invokeGuardedCallback (http://localhost:3000/static/js/bundle.js:16311:35)
ERROR
useNavigate() may be used only in the context of a <Router> component.
    at invariant (http://localhost:3000/static/js/bundle.js:1123:11)
    at useNavigateUnstable (http://localhost:3000/static/js/bundle.js:66522:102)
    at useNavigate (http://localhost:3000/static/js/bundle.js:66519:46)
    at App (http://localhost:3000/static/js/bundle.js:83:81)
    at renderWithHooks (http://localhost:3000/static/js/bundle.js:26618:22)
    at mountIndeterminateComponent (http://localhost:3000/static/js/bundle.js:29904:17)
    at beginWork (http://localhost:3000/static/js/bundle.js:31200:20)
    at beginWork$1 (http://localhost:3000/static/js/bundle.js:36163:18)
    at performUnitOfWork (http://localhost:3000/static/js/bundle.js:35432:16)
    at workLoopSync (http://localhost:3000/static/js/bundle.js:35355:9)

I am unable to find any solution for this problem.

These are the code that I am using in the files. app.js code:

import logo from './logo.svg';
import './App.css';
import LoginForm from './components/LoginForm';
import SignUp from './signup';
import { BrowserRouter as Router, Routes, Route, Link, useNavigate } from 'react-router-dom';

function App() {
  const navigate = useNavigate();

  const handleSignUpClick = () => {
    navigate('/signup');
  }

  return (
    <Router>
      <div className="Login">
        <h3>
          <a className="nav-link active" aria-current="page" href="#" onClick={handleSignUpClick}>
            Sign up
          </a>
        </h3>
        <Routes>
          <Route path="/" element={<LoginForm />} />
          <Route path="/signup" element={<SignUp />} />
        </Routes>
      </div>
    </Router>
  );
}

export default App;

This the code of LoginForm.js:

import React, { useState } from 'react';
import { Button, Form, FormGroup, Label, Input, InputGroup } from 'reactstrap';
import { FaEye, FaEyeSlash } from 'react-icons/fa';

function LoginForm() {
  const [username, setUsername] = useState('');
  const [password, setPassword] = useState('');
  const [showPassword, setShowPassword] = useState(false);

  const handleSubmit = (event) => {
    //handle submit
  }
  

  const toggleShowPassword = () => {
    setShowPassword(!showPassword);
  }

  return (
    <div className="form-box">
      <Form onSubmit={handleSubmit}>
        <FormGroup>
          <Label for="username">Username</Label>
          <Input type="text" name="username" id="username" value={username} onChange={e => setUsername(e.target.value)} />
        </FormGroup>
        <FormGroup>
          <Label for="password">Password</Label>
          <InputGroup>
            <Input type={showPassword ? 'text' : 'password'} name="password" id="password" value={password} onChange={e => setPassword(e.target.value)} />
            <Button onClick={toggleShowPassword}>
              {showPassword ? <FaEyeSlash /> : <FaEye />}
            </Button>
          </InputGroup>
        </FormGroup>
        <div className="text-center">
          <Button>Submit</Button>
        </div>
      </Form>
    </div>
  );
}

export default LoginForm;

This is the code of signup.js:

import './App.css';

function SignUp() {
  return (
    <div className="Login">
      <h1>Application</h1>
    </div>
  );
}

export default SignUp;

Solution

  • The useNavigate hook can't be used outside a routing context that is provided by a router, e.g. the BrowserRouter in this case. You could lift the BrowserRouter to be higher in the ReactTree to resolve this, but there's a much more trivial solution which is to just convert the raw anchor tag to a RRD Link

    <Link className="nav-link active" aria-current="page" to="/signup">
      Sign up
    </Link>
    
    function App() {
      return (
        <Router>
          <div className="Login">
            <h3>
              <Link className="nav-link active" aria-current="page" to="/signup">
                Sign up
              </Link>
            </h3>
            <Routes>
              <Route path="/" element={<LoginForm />} />
              <Route path="/signup" element={<SignUp />} />
            </Routes>
          </div>
        </Router>
      );
    }