gpt4 book ai didi

reactjs - react , react 路由器使用历史。 Uncaught Error : Invalid hook call

转载 作者:行者123 更新时间:2023-12-03 20:52:45 25 4
gpt4 key购买 nike

我只有一份 React,用 npm ls react 检查过, react 和 react-dom 是相同的版本,我没有违反任何钩子(Hook)规则,对吗?我不知道该怎么办了。

ERROR: Uncaught 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

我的组件:
import React from "react";
import { useHistory } from "react-router-dom";

export default function HomeButton() {
let history = useHistory();

function handleClick() {
history.push("/home");
}

return (
<button type="button" onClick={handleClick}>
Go home
</button>
);
}

包.json
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.5.0",
"@testing-library/user-event": "^7.2.1",
"axios": "^0.19.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1",
"styled-components": "^5.1.1"
},

最佳答案

解决了,
在 webpack.config.js 中添加解决以下问题:

 alias: {
react: path.resolve("./node_modules/react"),
}

我使用 create-react-app,所以我不得不运行 npm runeject,不建议这样做。有人对 future 有更好的想法吗?

关于reactjs - react , react 路由器使用历史。 Uncaught Error : Invalid hook call,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62157180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com