简体   繁体   English

错误:无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。 通过路由反应

[英]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.我是 React 的初学者,正在尝试做一些路由。

I'm trying to implement Route in React to redirect to another page, but everytime i insert, it shows error: Invalid hook call.我正在尝试在 React 中实现 Route 以重定向到另一个页面,但是每次我插入时,它都会显示错误:无效的钩子调用。

Here's the code from App.js这是来自 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 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和 EKGSim.js

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

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

            </h1>
        </div>
    );
}

export default EKGSim;

You need to use Route inside Routes try this您需要在Routes中使用Route试试这个

function App() {
  return (
    <Router>
      <div className="App">
        <Routes>
          <Route path="/ekgsim" element={<EKGSim/>} />
        </Routes>
      </div>
    </Router>
  );
}

Problem solved!问题解决了!

I just run this command npm install --save react-router-dom and it run perfectly!我只是运行这个命令npm install --save react-router-dom它运行完美!

I think the problem was that there was no react-router-dom in my package.json.我认为问题在于我的 package.json 中没有 react-router-dom。

Thanks for everyone's answer!谢谢大家的回答!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。 - 反应 - Error: Invalid hook call. Hooks can only be called inside of the body of a function component. - React 收到此错误“无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。” 在反应功能组件内? - Getting this error “Invalid hook call. Hooks can only be called inside of the body of a function component.” inside a react functional component? 错误:无效的挂钩调用。 钩子只能在函数组件的主体内部调用。 (带反应带) - Error: Invalid hook call. Hooks can only be called inside of the body of a function component. (w/ Reactstrap) 错误:无效的挂钩调用。 钩子只能在函数组件的主体内部调用。 使用 React.js 时 - Error: Invalid hook call. Hooks can only be called inside of the body of a function component. while using React.js React - 错误:无效的钩子调用。 钩子只能在 function 组件的主体内部调用。 我的语法有什么问题? - React - Error: Invalid hook call. Hooks can only be called inside of the body of a function component. What is wrong my syntax? 无效的钩子调用。 钩子只能在函数组件的主体内部调用。 如何解决这个问题? - Invalid hook call. Hooks can only be called inside of the body of a function component. How to solve this issue? 无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。 - 使用上下文 - Invalid hook call. Hooks can only be called inside of the body of a function component. - useContext 无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。 - 使用 state 时 - Invalid hook call. Hooks can only be called inside of the body of a function component. - When using state 无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。 有任何想法吗? - Invalid hook call. Hooks can only be called inside of the body of a function component. Any ideas? 反应钩子错误:无效的钩子调用。 钩子只能在 function 组件的主体内部调用 - React hook Error: Invalid hook call. Hooks can only be called inside of the body of a function component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM