Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

229
Views
Error: Invalid hook call. Hooks can only be called inside of the body of a function component. by Routing in react

I am a beginner with React and trying to do some Routing.

I'm trying to implement Route in React to redirect to another page, but everytime i insert , it shows error: Invalid hook call.

Here's the code from App.js

    import React from 'react';
    import './App.css';
    import Nav from './Nav';
    import EKGSim from './EKGSim';
    import { BrowserRouter as Router, Routes, Route} from 'react-router-dom';
    
    function App() {
      return(
        <Router>
        <div className='App'>
          <Nav />
          <Routes>
            <Route path="/ekgsim" element={<EKGSim/>} />
          </Routes>
        </div>
        </Router>
      )
    }
    
    export default App;

Can anyone tell me where the problem is? I've been trying for hours searching for the solution but came up to nothing. Any help will be appreciated! Thanks

Code for the Nav.js

    import React from 'react';
import './App.css';

function Nav(){
    return(
        <nav>
            <h3>Home</h3>
            <ul className='nav-links'>
                <li>Was Ist EKG?</li>
                <li>EKG Component</li>
                <li>EKG Simulator</li>
            </ul>
        </nav>
    );
}

export default Nav;

and the EKGSim.js

import React from 'react';
import './App.css';

function EKGSim(){
    return(
        <div>
            <h1>EKG Simulator

            </h1>
        </div>
    );
}

export default EKGSim;
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You need to use Route inside Routes try this

function App() {
  return (
    <Router>
      <div className="App">
        <Routes>
          <Route path="/ekgsim" element={<EKGSim/>} />
        </Routes>
      </div>
    </Router>
  );
}
about 4 years ago · Juan Pablo Isaza Report

0

Problem solved!

I just run this command npm install --save react-router-dom and it run perfectly!

I think the problem was that there was no react-router-dom in my package.json.

Thanks for everyone's answer!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!