【问题标题】:I tried using useHistory hook from react and it doesn;t work我尝试使用 react 中的 useHistory 钩子,但它不起作用;
【发布时间】:2021-11-13 13:34:25
【问题描述】:
App.js

import { Route, Router, Switch } from 'react-router-dom'
import Login from './Pages/Login';
import Profile from './Pages/Profile';
import Routes from './Routes1/Route';



function App() {
  return (
    <div className="App">
    <Routes />
    </div>
    
  );
}

export default App;

Route.js

import React from 'react';
import { Router, Route, Switch } from 'react-router-dom';
import { createBrowserHistory } from 'history';
import Login from '../Pages/Login';
import Profile from '../Pages/Profile';



const Routes = () => {
    const history = createBrowserHistory();
    return (
        <Router history={history}>
            <Switch>
                <Route path="/login" component={Login} />
                <Route path="/profile" component={Profile} />
            </Switch>
        </Router>
    );
}

export default Routes;

Login.js

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

function Login() {
        
        const history=useHistory();
        const handleHistory=() =>{
            history.push("/profile")
        }
    return (
        <div>
            <input type="text" placeholder="username" />
            <input typ="text" placeholder="password" />
            <button onClick={handleHistory}> Submit </button>
        </div>
    )
}

export default Login

Profile.js

import React from 'react'

function Profile() {
    return (
        <div>
            Congratulations! You have successfully logged in!
        </div>
    )
}

export default Profile

Package.json

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@material-ui/core": "^4.9.10",
    "@material-ui/icons": "^4.9.1",
    "@material-ui/lab": "^4.0.0-alpha.50",
    "@testing-library/jest-dom": "^5.12.0",
    "@testing-library/react": "^11.2.6",
    "@testing-library/user-event": "^13.1.5",
    "formik": "^2.1.4",
    "loadjs": "^4.2.0",
    "lodash": "^4.17.15",
    "history": "^5.0.0",
    "prop-types": "^15.7.2",
    "pure-react-carousel": "^1.27.0",
    "react": "^17.0.2",
    "react-addons-css-transition-group": "^15.6.2",
    "react-dom": "^17.0.2",
    "react-render-html": "^0.6.0",
    "react-router-dom": "^5.2.0",
    "react-scripts": "^4.0.3",
    "web-vitals": "^1.0.1",
    "yup": "^0.28.3"
  },
  
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

错误 错误:无效的挂钩调用。 Hooks 只能在函数组件的主体内部调用。这可能是由于以下原因之一:

  1. 您可能有不匹配的 React 版本和渲染器(例如 React DOM)
  2. 您可能违反了 Hooks 规则
  3. 您可能在同一个应用程序中拥有多个 React 副本 有关如何调试和解决此问题的提示,请参阅 https://reactjs.org/link/invalid-hook-call

【问题讨论】:

    标签: reactjs react-hooks react-router


    【解决方案1】:

    我不确定import { createBrowserHistory } from 'history';,但你的代码应该可以在没有它的情况下工作,并且BrowserRouter 作为路由器:

    Route.js
    
    import React from 'react';
    import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
    import Login from '../Pages/Login';
    import Profile from '../Pages/Profile';
    
    
    
    const Routes = () => {
        return (
            <Router>
                <Switch>
                    <Route path="/login" component={Login} />
                    <Route path="/profile" component={Profile} />
                </Switch>
            </Router>
        );
    }
    

    【讨论】:

    【解决方案2】:

    我在您的代码中看不到任何错误,但是,您不需要 Routes 组件中的历史记录来达到此目的,请尝试删除以下行:

    const history = createBrowserHistory() 
    

    和路由器中的历史道具。 或者不要创建handleSubmit 函数,而是尝试在onClick 处传递directly () =&gt; history.push()

    【讨论】:

    • 这应该是评论,而不是答案。
    猜你喜欢
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    • 2021-08-26
    • 2020-06-28
    • 1970-01-01
    • 2021-04-08
    • 2021-04-05
    • 1970-01-01
    相关资源
    最近更新 更多