[英]Navigate to welcome page after login page using React Router Dom v6
I'm a beginner learning React and using React v 17.0.2, react-router-dom v 6.0.2.我是学习 React 并使用 React v 17.0.2、react-router-dom v 6.0.2 的初学者。 I'm following a course made for react-router-dom v4.我正在学习为 react-router-dom v4 制作的课程。 I'm not able to get page navigation working if I try to navigate from a successful login to append a welcome message to the url.如果我尝试从成功登录导航到 append 向 url 发送欢迎消息,我无法使页面导航正常工作。 In v4 this is achieved by a {this.props.history.push("/welcome")
method.在 v4 中,这是通过{this.props.history.push("/welcome")
方法实现的。 I'm not able to something equivalent in V6.我无法在 V6 中实现同等功能。 Specifically, I would like to know how to handle the loginClicked method.具体来说,我想知道如何处理 loginClicked 方法。
Based on the helpful guidance from Himanshu Singh, I tried the following:根据 Himanshu Singh 的有用指导,我尝试了以下方法:
import { computeHeadingLevel } from '@testing-library/react'
import React, { Component } from 'react'
import { BrowserRouter as Router, Routes, Route, useNavigate } from 'react-router-dom'
class TodoApp extends Component {
render() {
return (
<div className="TodoApp">
<Router>
<Routes>
<Route path="/" exact element={<LoginComponent />} />
<Route path="/enlite" element={<LoginComponent />} />
<Route path="/welcome" element={<WelcomeComponent />} />
</Routes>
</Router>
{/* <LoginComponent /> */}
</div>
)
}
}
class WelcomeComponent extends Component {
render() {
return <div>Welcome to Enlite</div>
}
}
class LoginComponent extends Component {
constructor(props) {
super(props)
this.state = {
username: 'testuser',
password: '',
hasLoginFailed: false,
showSuccessMessage: false
}
this.handleChange = this.handleChange.bind(this)
this.loginClicked = this.loginClicked.bind(this)
}
handleChange(event) {
this.setState(
{
[event.target.name]
: event.target.value
})
}
**loginClicked() {
if (this.state.username === 'testuser' &&
this.state.password === 'dummy') {
function HandlePageNav() {
let navigate = useNavigate()
navigate('/welcome')
}
**HandlePageNav();**
}
else {
this.setState({ showSuccessMessage: false })
this.setState({ hasLoginFailed: true })
}
}**
render() {
return (
<div>
{this.state.hasLoginFailed && <div>Invalid Credentials</div>}
{this.state.showSuccessMessage && <div>Welcome to Enlite</div>}
User Name: <input type="text" name="username" value={this.state.username} onChange={this.handleChange} />
Password: <input type="password" name="password" value={this.state.password} onChange={this.handleChange} />
<button onClick={this.loginClicked}>Login</button>
</div>
)
}
}
export default TodoApp
This gives the following error:这给出了以下错误:
Error: Invalid hook call.错误:无效的挂钩调用。 Hooks can only be called inside of the body of a function component.钩子只能在 function 组件的主体内部调用。 This could happen for one of the following reasons:这可能由于以下原因之一而发生:
- You might have mismatching versions of React and the renderer (such as React DOM)你可能有不匹配的 React 版本和渲染器(例如 React DOM)
- You might be breaking the Rules of Hooks您可能违反了 Hooks 规则
- You might have more than one copy of React in the same app See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.您可能在同一个应用程序中拥有多个 React 副本,请参阅https://reactjs.org/link/invalid-hook-call以获取有关如何调试和修复此问题的提示。
Basically calling hooks in class components is not supported.基本上不支持在 class 组件中调用钩子。 I also tried to completely do away with the function like this:我还尝试像这样完全取消 function:
loginClicked() {
if (this.state.username === 'testuser' &&
this.state.password === 'dummy') {
let navigate = useNavigate()
navigate('/welcome')
}
else {
this.setState({ showSuccessMessage: false })
this.setState({ hasLoginFailed: true })
}
}
This gives a compile error:这给出了一个编译错误:
Line 85:32: React Hook "useNavigate" cannot be called in a class component.第 85:32 行:无法在 class 组件中调用 React Hook “useNavigate”。 React Hooks must be called in a React function component or a custom React Hook function react-hooks/rules-of-hooks Line 89:13: 'HandlePageNav' is not defined必须在 React function 组件或自定义 React Hook function react-hooks/rules-of-hooks Line 89:13: 'HandlePageNav' 中调用 React Hooks
no-undef无非定义
The above makes me wonder if I need to refactor my entire code into a function component or if there's a way to achieve this page navigation but keeping the class component.以上让我想知道是否需要将整个代码重构为 function 组件,或者是否有办法实现此页面导航但保留 class 组件。 Other than that I would appreciate any help or insights on this problem.除此之外,我将不胜感激有关此问题的任何帮助或见解。 Thanks in advance.提前致谢。
loginClicked() {
if (this.state.username === 'testuser' &&
this.state.password === 'dummy') {
/*You are not calling this function*/
function HandlePageNav() {
let navigate = useNavigate()
navigate('/welcome')
}
HandlePageNav(); /*Calling this function*/
}
else {
this.setState({ showSuccessMessage: false })
this.setState({ hasLoginFailed: true })
}
}
Or a much simpler way is, you don't need to create this HandlePageNav
function或者更简单的方法是,您不需要创建这个HandlePageNav
function
loginClicked() {
if (this.state.username === 'testuser' &&
this.state.password === 'dummy') {
/*keep it here, whenever login condition passes, it will automatically navigate*/
let navigate = useNavigate()
navigate('/welcome')
}
else {
this.setState({ showSuccessMessage: false })
this.setState({ hasLoginFailed: true })
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.