未捕获的运行时错误:useNavigate() 只能在 <Router> 组件的上下文中使用
P粉311563823
P粉311563823 2023-09-04 22:15:23
0
1
383
<p>我创建了一个新的反应项目。我是新来反应的,我试图在单击文本后进行导航,但它给了我这个错误。</p> <blockquote> <pre class="brush:php;toolbar:false;">Uncaught runtime errors: × ERROR useNavigate() may be used only in the context of a &lt;Router&gt; component. at invariant (http://localhost:3000/static/js/bundle.js:1123:11) at useNavigateUnstable (http://localhost:3000/static/js/bundle.js:66522:102) at useNavigate (http://localhost:3000/static/js/bundle.js:66519:46) at App (http://localhost:3000/static/js/bundle.js:83:81) at renderWithHooks (http://localhost:3000/static/js/bundle.js:26618:22) at mountIndeterminateComponent (http://localhost:3000/static/js/bundle.js:29904:17) at beginWork (http://localhost:3000/static/js/bundle.js:31200:20) at HTMLUnknownElement.callCallback (http://localhost:3000/static/js/bundle.js:16210:18) at Object.invokeGuardedCallbackDev (http://localhost:3000/static/js/bundle.js:16254:20) at invokeGuardedCallback (http://localhost:3000/static/js/bundle.js:16311:35) ERROR useNavigate() may be used only in the context of a &lt;Router&gt; component. at invariant (http://localhost:3000/static/js/bundle.js:1123:11) at useNavigateUnstable (http://localhost:3000/static/js/bundle.js:66522:102) at useNavigate (http://localhost:3000/static/js/bundle.js:66519:46) at App (http://localhost:3000/static/js/bundle.js:83:81) at renderWithHooks (http://localhost:3000/static/js/bundle.js:26618:22) at mountIndeterminateComponent (http://localhost:3000/static/js/bundle.js:29904:17) at beginWork (http://localhost:3000/static/js/bundle.js:31200:20) at HTMLUnknownElement.callCallback (http://localhost:3000/static/js/bundle.js:16210:18) at Object.invokeGuardedCallbackDev (http://localhost:3000/static/js/bundle.js:16254:20) at invokeGuardedCallback (http://localhost:3000/static/js/bundle.js:16311:35) ERROR useNavigate() may be used only in the context of a &lt;Router&gt; component. at invariant (http://localhost:3000/static/js/bundle.js:1123:11) at useNavigateUnstable (http://localhost:3000/static/js/bundle.js:66522:102) at useNavigate (http://localhost:3000/static/js/bundle.js:66519:46) at App (http://localhost:3000/static/js/bundle.js:83:81) at renderWithHooks (http://localhost:3000/static/js/bundle.js:26618:22) at mountIndeterminateComponent (http://localhost:3000/static/js/bundle.js:29904:17) at beginWork (http://localhost:3000/static/js/bundle.js:31200:20) at beginWork$1 (http://localhost:3000/static/js/bundle.js:36163:18) at performUnitOfWork (http://localhost:3000/static/js/bundle.js:35432:16) at workLoopSync (http://localhost:3000/static/js/bundle.js:35355:9)</pre> </blockquote> <p>我无法找到此问题的任何解决方案。</p> <p>这些是我在文件中使用的代码。 app.js代码:</p> <pre class="brush:php;toolbar:false;">import logo from './logo.svg'; import './App.css'; import LoginForm from './components/LoginForm'; import SignUp from './signup'; import { BrowserRouter as Router, Routes, Route, Link, useNavigate } from 'react-router-dom'; function App() { const navigate = useNavigate(); const handleSignUpClick = () =&gt; { navigate('/signup'); } return ( &lt;Router&gt; &lt;div className=&quot;Login&quot;&gt; &lt;h3&gt; &lt;a className=&quot;nav-link active&quot; aria-current=&quot;page&quot; href=&quot;#&quot; onClick={handleSignUpClick}&gt; Sign up &lt;/a&gt; &lt;/h3&gt; &lt;Routes&gt; &lt;Route path=&quot;/&quot; element={&lt;LoginForm /&gt;} /&gt; &lt;Route path=&quot;/signup&quot; element={&lt;SignUp /&gt;} /&gt; &lt;/Routes&gt; &lt;/div&gt; &lt;/Router&gt; ); } export default App;</pre> <p>这是LoginForm.js的代码:</p> <pre class="brush:php;toolbar:false;">import React, { useState } from 'react'; import { Button, Form, FormGroup, Label, Input, InputGroup } from 'reactstrap'; import { FaEye, FaEyeSlash } from 'react-icons/fa'; function LoginForm() { const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [showPassword, setShowPassword] = useState(false); const handleSubmit = (event) =&gt; { //handle submit } const toggleShowPassword = () =&gt; { setShowPassword(!showPassword); } return ( &lt;div className=&quot;form-box&quot;&gt; &lt;Form onSubmit={handleSubmit}&gt; &lt;FormGroup&gt; &lt;Label for=&quot;username&quot;&gt;Username&lt;/Label&gt; &lt;Input type=&quot;text&quot; name=&quot;username&quot; id=&quot;username&quot; value={username} onChange={e =&gt; setUsername(e.target.value)} /&gt; &lt;/FormGroup&gt; &lt;FormGroup&gt; &lt;Label for=&quot;password&quot;&gt;Password&lt;/Label&gt; &lt;InputGroup&gt; &lt;Input type={showPassword ? 'text' : 'password'} name=&quot;password&quot; id=&quot;password&quot; value={password} onChange={e =&gt; setPassword(e.target.value)} /&gt; &lt;Button onClick={toggleShowPassword}&gt; {showPassword ? &lt;FaEyeSlash /&gt; : &lt;FaEye /&gt;} &lt;/Button&gt; &lt;/InputGroup&gt; &lt;/FormGroup&gt; &lt;div className=&quot;text-center&quot;&gt; &lt;Button&gt;Submit&lt;/Button&gt; &lt;/div&gt; &lt;/Form&gt; &lt;/div&gt; ); } export default LoginForm;</pre> <p>这是signup.js的代码:</p> <pre class="brush:php;toolbar:false;">import './App.css'; function SignUp() { return ( &lt;div className=&quot;Login&quot;&gt; &lt;h1&gt;Application&lt;/h1&gt; &lt;/div&gt; ); } export default SignUp;</pre></p>
P粉311563823
P粉311563823

最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!