简体   繁体   English

如何修复 reactjs 中的历史推送?

[英]how can i fix the history push in reactjs?

i tried to put the history push in my login that after logging in, the page redirects it to another page, in my login it works but i tried to insert it also in the sign in page but unfortunately it does not redirect anything:我试图将历史推送放在我的登录中,登录后页面将其重定向到另一个页面,在我的登录中它可以工作,但我尝试将它也插入登录页面,但不幸的是它没有重定向任何内容:

Sign in page: (with axios)登录页面:(使用 axios)

import React, {useState, useEffect} from 'react'
import Axios from 'axios'
import './Register.css'
import styled from 'styled-components'
import Navbar from '../../Components/Navbar/Navbar'
import {useHistory} from 'react-router-dom'

function Register() {
const history = useHistory();

const [emailReg, setEmailReg] = useState("");
const [usernameReg, setUsernameReg] = useState("");
const [passwordReg, setPasswordReg] = useState("");
const [repeatpasswordReg, setRepeatPasswordReg] = useState("");
const [natReg, setNatReg] = useState("");
const [setregStatus, setRegStatus] = useState("");


Axios.defaults.withCredentials = true;

const register = () => {
  Axios.post("http://localhost:3001/register", {
    email: emailReg,
    username: usernameReg,
    password: passwordReg,
    nationality: natReg,
  }).then((response) => {
    history.push('/Login');
    console.log(response);
  })
  };
  return (
    <>
    <Navbar/>
     <Container>
      <div className="login-box1">
        <h1>Sign Up</h1>
            <div className="textbox1">
            <i class="fal fa-envelope"></i>
            <input type="text" placeholder="Email" onChange={(e) => {
        setEmailReg(e.target.value);
      }}/>
            </div>

            <div className="textbox1">
            <i class="fas fa-user-alien"></i>
            <input type="text" placeholder="Username" onChange={(e) => {
           setUsernameReg(e.target.value);
          }}/>
            </div>

            <div className="textbox1">
            <i class="fas fa-globe-americas"></i>
            <input type="text" placeholder="Nationality" onChange={(e) => {
          setNatReg(e.target.value);
          }}/>
            </div>

            <div className="textbox1">
            <i className="fas fa-lock"></i>
            <input type="password" placeholder="Password" onChange={(e) => {
          setPasswordReg(e.target.value);
         }} />
            </div>
            
            <div className="textbox1">
            <i className="fas fa-lock"></i>
            <input type="password" placeholder="Re-peat password" onChange={(e) => {
          setRepeatPasswordReg(e.target.value);
          }} />
            </div>
         <input type="button" class="btn11" value="Sign Up" onClick={register}/>
         </div>
         </Container>
        </>
        )}
         export default Register

as u can see, i defined the const history push and then i called it into the axios const, but unfortunatly it does not work, i could i fix that?如您所见,我定义了 const 历史推送,然后将其调用到 axios const 中,但不幸的是它不起作用,我可以修复它吗? thanks!!谢谢!!

Possible solutions可能的解决方案

  • Use withRouter to involve the components使用withRouter涉及组件

  • Create a browser history and access as a props创建浏览器历史记录并作为props访问

    <Router history={history}> {/*...Routes */} </Router
  • Check if the routes have an exact property correctly检查路线是否具有正确的exact属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM