简体   繁体   English

反应路由器useHistory上的无效挂钩调用

[英]Invalid hook call on react router useHistory

I'm aware some changes was made with react router library in the new version, though I think I followed the changes correctly and still receiving the following error我知道在新版本中对 react 路由器库进行了一些更改,但我认为我正确地遵循了更改并且仍然收到以下错误

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

Here is my code这是我的代码

import React, {useRef} from 'react';
import { useHistory } from 'react-router-dom';
function Home() {
    const history = useHistory(); //This hook causing the error
    const RouteAbout = () => {
      history.push("/about");
    }
    return(<> 
       <button onClick={RouteAbout}> test </button>
    </>);
}

The error refers to错误是指

const history = useHistory();

that imported from从进口的

import { useHistory } from 'react-router-dom';

How can I apply useHistory hook with react router in a valid way?如何以有效的方式将useHistory钩子与反应路由器一起应用?

EDIT:编辑:

App.JS应用程序.JS

function App() {
  return (
    <>
    <Router>
        <Header/>
        <Switch>
          <Route exact path="/">
            <Home />
          </Route>
          <Route path="/about">
            <About />
          </Route>
          <Route path="/portfolio">
            <Portfolio />
          </Route>
          <Route path="/contact">
            <Contact />
          </Route>
        </Switch>
    </Router>
    <Footer/>
    </>
  );
}

Versions版本

    "history": "^5.0.0",
    "node-sass": "^5.0.0",
    "react": "^16.14.0",
    "react-dom": "^17.0.2",
    "react-perf-devtool": "^3.1.8",
    "react-router-dom": "^5.2.0",
    "react-scripts": "^4.0.3",
    "web-vitals": "^1.0.1"

Are you sure to have wrapped your application inside a BrowserRouter ?您确定将您的应用程序包装在BrowserRouter吗?

This code works for me.这段代码对我有用。

import ReactDOM from "react-dom";
import React from "react";
import { useHistory, BrowserRouter } from "react-router-dom";

function App() {
  let history = useHistory();
  const RouteAbout = () => {
    history.push("/about");
  };
  return (
    <>
      <button onClick={RouteAbout}> test </button>
    </>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(
    <BrowserRouter>
    <App />
    </BrowserRouter>,
  rootElement
);

Didn't found what causing this error, but found an alternative.没有找到导致此错误的原因,但找到了替代方法。

Using NavLink although the conflict of link & button as HTML tags worked for me.使用NavLink虽然链接和按钮的冲突作为 HTML 标签对我有用。 A good solution will be to remove the <button> tag and "fake" one using <a> tag which provided by NavLink .一个好的解决方案是使用NavLink提供的<a>标签删除<button>标签和“假”标签。

<NavLink to="/about" onClick={() => window.scrollTo(0, 0)}><button>test</button></NavLink>

or或者

<NavLink to="/about" onClick={() => window.scrollTo(0, 0)}>test</NavLink>

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

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