惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LINUX DO - 热门话题
Stack Overflow Blog
Stack Overflow Blog
B
Blog
WordPress大学
WordPress大学
Project Zero
Project Zero
P
Palo Alto Networks Blog
阮一峰的网络日志
阮一峰的网络日志
博客园 - 司徒正美
有赞技术团队
有赞技术团队
S
SegmentFault 最新的问题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
T
Tailwind CSS Blog
Forbes - Security
Forbes - Security
F
Full Disclosure
SecWiki News
SecWiki News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hacker News: Ask HN
Hacker News: Ask HN
C
Check Point Blog
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Fortinet All Blogs
Cisco Talos Blog
Cisco Talos Blog
G
Google Developers Blog
J
Java Code Geeks
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recorded Future
Recorded Future
O
OpenAI News
Spread Privacy
Spread Privacy
MongoDB | Blog
MongoDB | Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
Cybersecurity and Infrastructure Security Agency CISA
S
Securelist
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
IT之家
IT之家
U
Unit 42
腾讯CDC
S
Security Affairs
C
Cisco Blogs
Schneier on Security
Schneier on Security
The Last Watchdog
The Last Watchdog
B
Blog RSS Feed
宝玉的分享
宝玉的分享
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security @ Cisco Blogs
Cyberwarzone
Cyberwarzone
T
The Blog of Author Tim Ferriss

Deed's博客

解决群晖百度套件提示name "baiduapp" dupliacted - Deed's博客 2023年3月16日 Maven项目引入第三方本地jar包 - Deed's博客 2022年8月23日nginx反向代理配置去除前缀 - Deed's博客 2022年8月1日 ts-node 运行报错`Cannot find name 'console'.` 2022年7月13日 Gradle项目打包相关配置 - Deed's博客 2022年6月23日 Windows 杀死进程 - Deed's博客 2022年6月16日 查看服务器异常IP访问以及处理 - Deed's博客 2022年6月15日 Linux删除用户 - Deed's博客 2022年6月8日 AntDesignPro文档Demo示例[后置拦截器]报错 - Deed's博客
2022年6月8日 React使用umi.js报错Invalid hook call - Deed's博客
Mahalalel · 2022-06-08 · via Deed's博客
«

Mahalalel 发布于 阅读:5606 React


报错详情

在写React项目的Demo的时候,使用umi中useHistory,调用运行时,报了以下错误

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

翻译如下:

无效的挂钩调用。 钩子只能在函数组件的主体内部调用。 这可能由于以下原因之一而发生:
1. React 和渲染器的版本可能不匹配(例如 React DOM)
2. 你可能违反了 Hooks 规则
3. 你可能在同一个应用程序中拥有多个 React 副本

代码

import { doLogin } from '@/api/user/user';
import { Button, Form, Input, message } from 'antd';
import { useHistory } from 'umi';

const history = useHistory();
const login = (user: API.IUser) => {
  doLogin(user).then(res => {
    console.log(res);

    if (res.code === 200) {
      message.success({
        content: "用户" + user.username + "登录成功",
        onClose: () => {
          history.push("/")
        }
      });
    } else {
      message.error("登录失败");
    }
  })
}

function Index() {
  return (
    <>
      <Form
        onFinish={login}
        labelCol={{ span: 6 }}
        wrapperCol={{ span: 12 }}
      >
        <Form.Item
          label={'用户名'} name={'username'}
          rules={[
            {
              required: true,
              type: 'string',
              message: '用户名不允许为空'
            }
          ]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          label={'密码'} name={'password'}
          rules={[
            {
              required: true,
              type: 'string',
              message: '密码不允许为空'
            },
            {
              max: 20,
              min: 10,
              message: '密码长度在10~20之间'
            },
            {

            }
          ]}
        >
          <Input.Password />
        </Form.Item>
        <Form.Item wrapperCol={{ offset: 6, span: 12 }}>
          <Button type="primary" htmlType="submit">
            登录
          </Button>
          <Button type="default" style={{ marginLeft: 10 }} htmlType="reset">
            重置
          </Button>
        </Form.Item>
      </Form>
    </>
  );
}

export default Index;

解决

排查后发现,原来是
const history = useHistory();
这句初始化代码写在了组件外面,导致异常。

底层逻辑如下图
react-hooks

截图来自链接:react-hooks原理

将代码移入组件内部,错误解决了,代码如下


import { doLogin } from '@/api/user/user';
import { Button, Form, Input, message } from 'antd';
import { useHistory } from 'umi';

function Index() {
  const history = useHistory();
  const login = (user: API.IUser) => {
    doLogin(user).then(res => {
      console.log(res);

      if (res.code === 200) {
        message.success({
          content: "用户" + user.username + "登录成功",
          onClose: () => {
            history.push("/")
          }
        });
      } else {
        message.error("登录失败");
      }
    })
  }
  ...
}

export default Index;

React Hooks报错