javascript - 重定向而不重新加载页面
问题描述
我正在做一个登录页面,我希望当这个人正确登录时将他重定向到一个页面但没有重新加载,我尝试使用 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')
}
})
}
我不知道为什么它对我不起作用,但如果有人知道修复它的方法或不更新重定向的库,请帮助我
解决方案
推荐阅读
- python - Python:为什么当我将 int 或 list 或 tuple 放入列表时结果相同?
- javascript - 添加 javascript 查询动态更改 Oracle Apex 中的按钮标签
- python - Python字符串比较失败
- javascript - 旋转数字问题 - 为什么采用 n mod 10 和 n /10?
- linq - EF Core Complex 查询为每个参数变体缓存
- android - 在生产代码中使用 android library security-crypto 的候选版本
- matrix - 如何在 sprase 矩阵中使用 groupby 和 mean?
- javascript - 如何提高agora音质
- asp.net - 如何在子目录 asp.net 内的 web.config 中设置 bin 文件夹的路径
- python - 如何找到零的子区间