简体   繁体   English

无效的挂钩调用。 钩子只能在反应 18 中的函数组件的主体内部调用

[英]Invalid hook call. Hooks can only be called inside of the body of a function component in react 18

I am new in react facing some issues in hooks can't able to resolve the problem.我是新手,面对钩子中的一些问题无法解决问题。 Can anybody help me.有谁能够帮我。

react.development.js:207 Warning: Invalid hook call. react.development.js:207 警告:无效的钩子调用。 Hooks can only be called inside of the body of a function component.钩子只能在函数组件的主体内部调用。 This could happen for one of the following reasons:这可能由于以下原因之一而发生:

  1. You might have mismatching versions of React and the renderer (such as React DOM)你可能有不匹配的 React 版本和渲染器(例如 React DOM)
  2. You might be breaking the Rules of Hooks您可能违反了 Hooks 规则
  3. You might have more than one copy of React in the same app See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem您可能在同一个应用程序中拥有多个 React 副本 有关如何调试和修复此问题的提示,请参阅https://reactjs.org/link/invalid-hook-call

App.js

import NavBar from "./components/NavBar";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import { Home } from "./components/Pages/Home";
import { About } from "./components/Pages/About";
import { Blog } from "./components/Pages/Blog";
import { Contact } from "./components/Pages/Contact";


function App() {
  return (
      <Router>
        <NavBar />

        <div className="pages">
          <Routes>
            <Route exact path="/" element={<Home/>} />
            <Route path="/about" element={<About/>} />
            <Route path="/blog" element={<Blog/>} />
            <Route path="/contact" element={<Contact/>} />
          </Routes>
        </div>
      </Router>
  );
}
export default App;

ERROR enter image description here ERROR在此处输入图像描述

Navbar.js

import React, { useState } from "react";
import { NavLink } from "react-router-dom";
import "./NavBar.css";

NavBar()

export default function NavBar() {
  const [click, setClick] = useState(false);

  const handleClick = () => setClick(!click);
  return (
    <>
      <nav className="navbar">
        <div className="nav-container">
          <NavLink exact to="/" className="nav-logo">
            CodeBucks
            <i className="fas fa-code"></i>
          </NavLink>

          <ul className={click ? "nav-menu active" : "nav-menu"}>
            <li className="nav-item">
              <NavLink
                exact
                to="/"
                activeClassName="active"
                className="nav-links"
                onClick={handleClick}
              >
                Home
              </NavLink>
            </li>
            <li className="nav-item">
              <NavLink
                exact
                to="/about"
                activeClassName="active"
                className="nav-links"
                onClick={handleClick}
              >
                About
              </NavLink>
            </li>
            <li className="nav-item">
              <NavLink
                exact
                to="/blog"
                activeClassName="active"
                className="nav-links"
                onClick={handleClick}
              >
                Blog
              </NavLink>
            </li>
            <li className="nav-item">
              <NavLink
                exact
                to="/contact"
                activeClassName="active"
                className="nav-links"
                onClick={handleClick}
              >
                Contact Us
              </NavLink>
            </li>
          </ul>
          <div className="nav-icon" onClick={handleClick}>
            <i className={click ? "fas fa-times" : "fas fa-bars"}></i>
          </div>
        </div>
      </nav>
    </>
  );
}

if i remove navbar() from navbar.js more errors are showing enter image description here如果我从 navbar.js 中删除 navbar() 会显示更多错误,请在此处输入图像描述

package.json enter image description here package.json在此处输入图片描述

index.js index.js

import React from 'react';
import ReactDOM from 'react-dom/client';
// import './index.css';
import App from './App';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
    <App />
);

You called NavBar() inside your file.您在文件中调用了NavBar()

NavBar()

export default function NavBar() {

You are calling the component like the general function in the Navbar.js.您正在像 Navbar.js 中的通用函数一样调用组件。

import React, { useState } from "react";
import { NavLink } from "react-router-dom";
import "./NavBar.css";
[enter image description here][1]
// Please remove this line
// NavBar()

export default function NavBar() {
  const [click, setClick] = useState(false);

  const handleClick = () => setClick(!click);
  return (
    <>
      <nav className="navbar">
        <div className="nav-container">
          <NavLink exact to="/" className="nav-logo">
            CodeBucks
            <i className="fas fa-code"></i>
          </NavLink>

          <ul className={click ? "nav-menu active" : "nav-menu"}>
            <li className="nav-item">
              <NavLink
                exact
                to="/"
                activeClassName="active"
                className="nav-links"
                onClick={handleClick}
              >
                Home
              </NavLink>
            </li>
            <li className="nav-item">
              <NavLink
                exact
                to="/about"
                activeClassName="active"
                className="nav-links"
                onClick={handleClick}
              >
                About
              </NavLink>
            </li>
            <li className="nav-item">
              <NavLink
                exact
                to="/blog"
                activeClassName="active"
                className="nav-links"
                onClick={handleClick}
              >
                Blog
              </NavLink>
            </li>
            <li className="nav-item">
              <NavLink
                exact
                to="/contact"
                activeClassName="active"
                className="nav-links"
                onClick={handleClick}
              >
                Contact Us
              </NavLink>
            </li>
          </ul>
          <div className="nav-icon" onClick={handleClick}>
            <i className={click ? "fas fa-times" : "fas fa-bars"}></i>
          </div>
        </div>
      </nav>
    </>
  );
}

React Component should not be called like a function. React 组件不应该像函数一样被调用。 That should settled inside of DOM element.这应该在 DOM 元素内部解决。

暂无
暂无

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

相关问题 React Hooks + Mobx =&gt; 无效的钩子调用。 Hooks 只能在函数组件内部调用 - React Hooks + Mobx => Invalid hook call. Hooks can only be called inside of the body of a function component React Hooks Mobx:无效的钩子调用。 钩子只能在函数组件的主体内部调用 - React Hooks Mobx: invalid hook call. Hooks can only be called inside of the body of a function component 反应钩子错误:无效的钩子调用。 钩子只能在 function 组件的主体内部调用 - React hook Error: Invalid hook call. Hooks can only be called inside of the body of a function component React Native 错误:无效的钩子调用。 钩子只能在 function 组件的主体内部调用 - React Native Error: Invalid hook call. Hooks can only be called inside of the body of a function component 错误:无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。 通过路由反应 - Error: Invalid hook call. Hooks can only be called inside of the body of a function component. by Routing in react 错误:无效的挂钩调用。 钩子只能在 react-native 中的 function 组件的主体内部调用 - Error: Invalid hook call. Hooks can only be called inside of the body of a function component in react-native 反应无效的钩子调用。 钩子只能在 function 组件的主体内部调用 - React Invalid hook call. Hooks can only be called inside of the body of a function component 错误:无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。 - 反应 - Error: Invalid hook call. Hooks can only be called inside of the body of a function component. - React 反应 Redux - 错误:无效的挂钩调用。 钩子只能在 function 组件的主体内部调用 - React Redux - Error: Invalid hook call. Hooks can only be called inside of the body of a function component React - 无效的钩子调用。 钩子只能在 function 组件的主体内部调用 - React -Invalid hook call. Hooks can only be called inside of the body of a function component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM