简体   繁体   English

TypeScript React 无效的挂钩调用错误(“错误:无效的挂钩调用。”)

[英]TypeScript React Invalid Hook Call Error ("Error: Invalid hook call.")

I tried asking a question earlier but realized it was a bit all over the place so no one could really understand it properly.我之前试着问一个问题,但意识到它有点到处都是,所以没有人能真正正确地理解它。 So here's a more clarified attempt.所以这里有一个更明确的尝试。

I am currently working on a CRUD web project that allows the user to edit, view and create more users which is then displayed on a table.我目前正在开发一个 CRUD Web 项目,该项目允许用户编辑、查看和创建更多用户,然后将这些用户显示在表格上。

I have been using react-bootstrap's components so decided to use the Modal component provided.我一直在使用 react-bootstrap 的组件,所以决定使用提供的 Modal 组件。 ( https://react-bootstrap.netlify.com/components/modal/#modals-live ) ( https://react-bootstrap.netlify.com/components/modal/#modals-live )

It was successfully working without any errors when I just called and used the component within App.tsx as follows:当我刚刚调用并使用 App.tsx 中的组件时,它成功运行,没有任何错误,如下所示:

const App: React.FC = () => {
  const [show, setShow] = useState(false);
  const handleClose = () => setShow(false);
  const handleShow = () => setShow(true);

  return (
    <div className="App">
  <NavBar/>
  <Jumbotron fluid className="userJumbotron">
      <Container>
        <h1>Manage Users</h1>
        <Button variant="outline-dark" onClick={handleShow}>Add new user</Button>
          <Modal show={show} onHide={handleClose}>
            <Modal.Header closeButton>
              <Modal.Title>Add User</Modal.Title>
            </Modal.Header>
        <Modal.Body><NewUserForm/></Modal.Body>
        <Modal.Footer>
          <Button variant="secondary" onClick={handleClose}>
            Close
          </Button>
          <Button variant="primary" onClick={handleClose}>
            Save Changes
          </Button>
        </Modal.Footer>
      </Modal>

But I think realized that I'll need more than 1 modal component for my program and decided to make the modal component section into my own component that I can reuse as many times as I want and customize as I want within the App.tsx但我认为我意识到我的程序需要 1 个以上的模态组件,并决定将模态组件部分制作成我自己的组件,我可以根据需要多次重复使用,并在 App.tsx 中根据需要进行自定义

So I decided to make a NewModal.tsx component that has the bootstrap modal component and button pre-made meaning I'd only have to call one line () rather than a whole chunk.所以我决定制作一个 NewModal.tsx 组件,它具有引导程序模态组件和按钮预制,这意味着我只需要调用一行 () 而不是整个块。 the NewModal.tsx code is as follows: NewModal.tsx 代码如下:

export default class NewModal extends Component<any> {
  constructor (props: any){
    super(props)
    this.state={
      show:false
    }
  }
    render() {
  const [show, setShow] = useState(false);
  const handleClose = () => setShow(false);
  const handleShow = () => setShow(true);

      return (
        <div>

    <h1>Manage Users</h1>
    <Button variant="outline-dark" onClick={handleShow}>Add new user</Button>
      <Modal show={show} onHide={handleClose}>
        <Modal.Header closeButton>
          <Modal.Title>Add User</Modal.Title>
        </Modal.Header>
        <Modal.Body><NewUserForm/></Modal.Body>
        <Modal.Footer>
          <Button variant="secondary" onClick={handleClose}>
            Close
          </Button>
          <Button variant="primary" onClick={handleClose}>
            Save Changes
          </Button>
        </Modal.Footer>
      </Modal>
</div>
      );
    }
  }

I am getting the following error from this code.我从此代码中收到以下错误。 在此处输入图片说明

What is the cause for this error?这个错误的原因是什么?

You can't use hooks inside class components, you need to change it to be a function based component, which can look something like this:您不能在类组件中使用钩子,您需要将其更改为基于函数的组件,它可能如下所示:

const NewModal = () => {
  const [show, setShow] = useState(false);
  const handleClose = () => setShow(false);
  const handleShow = () => setShow(true);

  return (
    <div>
      <h1>Manage Users</h1>
      <Button variant="outline-dark" onClick={handleShow}>
        Add new user
      </Button>
      <Modal show={show} onHide={handleClose}>
        <Modal.Header closeButton>
          <Modal.Title>Add User</Modal.Title>
        </Modal.Header>
        <Modal.Body>
          <NewUserForm />
        </Modal.Body>
        <Modal.Footer>
          <Button variant="secondary" onClick={handleClose}>
            Close
          </Button>
          <Button variant="primary" onClick={handleClose}>
            Save Changes
          </Button>
        </Modal.Footer>
      </Modal>
    </div>
  );
}

export default NewModal;

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

相关问题 未捕获的错误:无效的挂钩调用。 ReactJs - Uncaught Error: Invalid hook call. ReactJs 如何修复 React(TypeScript) 错误“无效的钩子调用。”? - How to fix React(TypeScrtipt) error “Invalid hook call.”? 反应钩子,发生无效钩子调用错误 - React hook, Invalid hook call error occurs 无效的钩子调用。 React.js - Invalid hook call. React.js 反应钩子错误:无效的钩子调用。 钩子只能在 function 组件的主体内部调用 - React hook Error: Invalid hook call. Hooks can only be called inside of the body of a function component 错误:无效的挂钩调用。 伙计们请帮帮我 - Error: Invalid hook call. guys please helpme 未捕获(承诺中)错误:无效的挂钩调用。 - 特例 - Uncaught (in promise) Error: Invalid hook call. - SPECIAL CASE 反应 - 无效的钩子调用。 未捕获的错误:缩小的 React 错误 #321 - React - invalid hook call. Uncaught Error: Minified React error #321 React 17:错误:无效的钩子调用。 钩子只能在 function 组件的主体内部调用 - React 17: 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM