Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 提交登录和注册按钮后如何重定向到React中的配置文件页面?_Reactjs_Redirect_React Router - Fatal编程技术网

Reactjs 提交登录和注册按钮后如何重定向到React中的配置文件页面?

Reactjs 提交登录和注册按钮后如何重定向到React中的配置文件页面?,reactjs,redirect,react-router,Reactjs,Redirect,React Router,以下是我的登录页面代码,我想用它重定向到配置文件页面: import React, { Component } from "react"; export default class Login extends Component { constructor(props){ super(props) this.state = { username: '', password: '

以下是我的登录页面代码,我想用它重定向到配置文件页面:

import React, { Component } from "react";


export default class Login extends Component {
   
    constructor(props){
        super(props)
        this.state = {
            username: '',
            password: '',
        }
    }
    handleChange = (e) => {
        this.setState({ [e.target.name]: e.target.value });
    }

    handleSubmit = (e) => {
        e.preventDefault();
        const {username, password} = this.state;

        fetch('http://localhost:9000/users/login', {
            method: "POST",
            headers: {
            'Content-Type' : 'application/json'
        },
            body: JSON.stringify(this.state),
            
        })
        .then((result) => result.json())
        .then((info) => {console.log(info)})
        
    }

    render() {
        return (
        <div className="auth-wrapper">
          <div className="auth-inner">
             <form onSubmit={this.handleSubmit}>
                <h3>Sign In</h3>

                <div className="form-group">
                    <label>Username</label>
                    <input type="text" className="form-control" placeholder="Enter email" value={this.state.value} onChange={this.handleChange} name="username" />
                </div>

                <div className="form-group">
                    <label>Password</label>
                    <input type="password" name="password" className="form-control" value={this.state.value} onChange={this.handleChange} placeholder="Enter password" />
                </div>

                <div className="form-group">
                    <div className="custom-control custom-checkbox">
                        <input type="checkbox" className="custom-control-input" id="customCheck1" />
                        <label className="custom-control-label" htmlFor="customCheck1">Remember me</label>
                    </div>
                </div>

                <button type="submit" className="btn btn-primary btn-block">Submit</button>
                <p className="forgot-password text-right">
                    Forgot <a href="#">password?</a>
                </p>
            </form>
          </div>
        </div>
        );
    }
}
import React,{Component}来自“React”;
导出默认类登录扩展组件{
建造师(道具){
超级(道具)
此.state={
用户名:“”,
密码:“”,
}
}
handleChange=(e)=>{
this.setState({[e.target.name]:e.target.value});
}
handleSubmit=(e)=>{
e、 预防默认值();
const{username,password}=this.state;
取('http://localhost:9000/users/login', {
方法:“张贴”,
标题:{
“内容类型”:“应用程序/json”
},
正文:JSON.stringify(this.state),
})
.then((result)=>result.json())
.then((info)=>{console.log(info)})
}
render(){
返回(
登录
用户名
密码
记得我吗
提交

忘记

); } }
import React,{Component}来自“React”;
从“react router”导入{Redirect};
从“react router”导入{withRouter};
导出默认类注册扩展组件{
建造师(道具){
超级(道具)
此.state={
名字:'',
密码:“”,
用户名:“”,
姓氏:“”,
电子邮件:“”,
伊萨明:“错”,
}
}
onChange=(e)=>{
this.setState({[e.target.name]:e.target.value});
console.log(this.state.email)
console.log(this.state.isAdmin)
}
onSubmit=async(e)=>{
e、 预防默认值();
等待this.state.isAdmin==“on”?this.setState({isAdmin:true}):this.setState({isAdmin:false})
//const{firstName,lastName,email,}=this.state;
取('http://localhost:9000/users/new', {
方法:“张贴”,
标题:{
“内容类型”:“应用程序/json”
},
正文:JSON.stringify(this.state)
})
.then((info)=>{console.log(info)})
this.props.history.push('/profile');
}
render(){
返回(
注册
名字
姓
用户名
电子邮件地址
密码
注册为审核人
注册

已注册

); } }
当我点击我的个人资料页面时,我想重定向到/profile。此时它挂起并成功登录用户。我现在必须手动转到/profile才能访问该页面 我正在使用Express作为我的后端供您参考

编辑:我已经添加了我的注册页面代码,因为这不起作用,也不知道为什么。与登录页面一样,它也会挂起

 fetch('http://localhost:9000/users/login', {
        method: "POST",
        headers: {
        'Content-Type' : 'application/json'
    },
        body: JSON.stringify(this.state),
        
    })
    .then((result) => {
     result.json();
     this.props.history.push("/Profile");})
    .then((info) => {console.log(info)})
  
 

这回答了你的问题吗?很好,它起作用了,但我实际上在注册页面上也遇到了同样的问题,并且用注册页面更新了帖子。我使用了与您在登录中建议的相同的代码,并将其放入注册中,但它不起作用。put this.props.history.push(“/Profile”);})你的意思是这样吗。然后((info)=>{this.props.history.push('/profile')})。因为这似乎不起作用。然后((result)=>{result.json();this.props.history.push(“/Profile”);})
 fetch('http://localhost:9000/users/login', {
        method: "POST",
        headers: {
        'Content-Type' : 'application/json'
    },
        body: JSON.stringify(this.state),
        
    })
    .then((result) => {
     result.json();
     this.props.history.push("/Profile");})
    .then((info) => {console.log(info)})