我正在做一个登录页面,我希望当这个人正确登录时将他重定向到一个页面但不重新加载,我试图用 useHistory 来做到这一点,但它对我不起作用
import React, { Component } from 'react'
import './main.css';
import {BrowserRouter as Router, Switch, Route, Link, Redirect,
useHistory,
useLocation, browserHistory, withRouter} from 'react-router-dom';
import Register from './components/ComponentRegister'
import Login from './components/ContainerLogin'
import ChatPrincipal from './Chat/ChatPrincipal'
活动代码
login = (username, password) =>{
const history = useHistory()
fetch('/api/login', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({username: username, password: password})
})
.then(r => r.json())
.then(data =>{
console.log(data.status)
if(data.status === 200){
console.log('si')
this.setState({
status: data.status,
message: data.message,
username: username
})
this.userSearch()
// window.location.href = "/"
history.push('/') <===================================
}else{
this.setState({
messageError: data.message,
status: data.status
})
console.log('Error')
const error = document.getElementById('Error')
}
})
}
我不知道为什么它对我不起作用,但是如果有人知道修复它的方法或无需更新即可重定向的库,请帮助我
答案 0 :(得分:0)