简体   繁体   English

无效的钩子调用。 反应堆

[英]Invalid hook call. ReacJs

I have been troubleshooting this error for hours now.我已经解决了这个错误几个小时了。 I'm using the hooks on top of a function (getItems).我在函数(getItems)之上使用钩子。 i don't know what mistake i have done.我不知道我犯了什么错误。 How should i clear this?我应该如何清除这个?

ERROR: `错误:`

Error: Invalid hook call.错误:无效的挂钩调用。 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) 2. You might be breaking the Rules of 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.这可能是由于以下原因之一而发生的: 1. 你可能有不匹配的 React 版本和渲染器(例如 React DOM) 2. 你可能违反了 Hooks 规则 3. 你可能有多个 React 副本同一个应用程序 请参阅https://reactjs.org/link/invalid-hook-call以获取有关如何调试和解决此问题的提示。

` `

App.js应用程序.js

import React from "react";

const data = {
list: [
  {
    id: 0,
    title: "A1",
    list: [
      {
        id: 3,
        title: "A2",
      },
    ]
  },
]
 };
  function getItems() {
    const [menuStack, setStack] = React.useState([data.list]);

    const pushState = (list) => {
        list && setStack((stack) => [...stack, list]);
    };
    const popState = () => {
        menuStack.length > 1 && setStack((stack) => stack.slice(0, -1));
    };

    const top = menuStack[menuStack.length - 1];
    return (
        <button onClick={popState}>BACK</button>
    );

}
export default class PopUp extends React.Component {
render() {
    return (
        <div>
            {getItems()}
        </div>
    );
}
}

Index.js索引.js

import React from "react";
export default class Home extends React.Component {
render(){
 return (
 <App />
  );
 }
} 

You are calling function getItems inside your PopUp component and expecting to print the output.您正在 PopUp 组件内调用函数 getItems 并希望打印输出。 React treat that function as a normal function and hence throws that error. React 将该函数视为普通函数,因此会抛出该错误。

You need to tell react that this is a component function by doing你需要通过做来告诉反应这是一个组件功能

<div>
<getItems />
</div>

The issue is with class getItems, You can use that as a Component.问题在于类 getItems,您可以将其用作组件。 <GetItems />

import React from "react";

const data = {
  list: [
    {
      id: 0,
      title: "A1",
      list: [
        {
          id: 3,
          title: "A2",
        },
      ],
    },
  ],
};
function GetItems() {
  const [menuStack, setStack] = React.useState([data.list]);

  const pushState = (list) => {
    list && setStack((stack) => [...stack, list]);
  };
  const popState = () => {
    menuStack.length > 1 && setStack((stack) => stack.slice(0, -1));
  };

  const top = menuStack[menuStack.length - 1];
  return <button onClick={popState}>BACK</button>;
}

export default class PopUp extends React.Component {
  render() {
    return (
      <div>
        <GetItems />
      </div>
    );
  }
}

In react, hooks can be written in functional components.在 React 中,钩子可以写在功能组件中。 But you called getItems function as a general function, not a functional component.但是您将getItems函数称为通用函数,而不是函数组件。

You should let react treat as a component like following:您应该让 react 视为一个组件,如下所示:

<getItems />

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM