• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Uncaught TypeError: Cannot read properties of undefined (reading ‘push‘)和Uncaught TypeError: Canno

武飞扬头像
BUG何处藏
帮助1

学习React视频的时候遇到教程使用老版导致路由的跳转和返回都和新版不一样而报错,找了很久资料发现可以这样使用而不是使用原来的props.history.go(-1)和this.props.history.push('/home')

  1.  
    import React from 'react';
  2.  
    import {createRoot} from 'react-dom/client';
  3.  
    import {BrowserRouter as Router, Route, Link, Routes, useNavigate} from 'react-router-dom'
  4.  
     
  5.  
    const Login=()=>{
  6.  
    const navigate = useNavigate();
  7.  
    return (
  8.  
    <div>
  9.  
    <p>登录页面</p>
  10.  
    <button onClick={()=> navigate('/home')}>登录</button>
  11.  
    </div>
  12.  
    )
  13.  
    }
  14.  
    const Home=()=>{
  15.  
    const navigate = useNavigate();
  16.  
    const handleBack =()=>{
  17.  
    navigate(-1)
  18.  
    }
  19.  
    return (
  20.  
    <div>
  21.  
    <p>我是主页</p>
  22.  
    <button onClick={handleBack}>返回登录页面按钮</button>
  23.  
    </div>
  24.  
    )
  25.  
    }
  26.  
    const App = () => (
  27.  
    <Router>
  28.  
    <div>
  29.  
    <h1>React路由基础</h1>
  30.  
    {/* 指定路由入口,点击哪里跳转*/}
  31.  
    <Link to="/login">去登陆</Link>
  32.  
    {/* 指定路由出口,跳转到哪里去*/}
  33.  
    <Routes>
  34.  
    <Route path="/home" element={<Home />} />
  35.  
    <Route path="/login" element={<Login />}/>
  36.  
    </Routes>
  37.  
    </div>
  38.  
    </Router>
  39.  
    )
  40.  
    createRoot(document.getElementById('root')).render(<App/>)
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfhegi
系列文章
更多 icon
同类精品
更多 icon
继续加载