Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

javascript - 在react.js中重定向(Redirect in react.js)

I am new in react.js.

(我是react.js的新手。)

How to do redirect in react.js after login ?

(登录后如何在react.js中重定向?)

Is it different from Vue.js routing ?

(与Vue.js路由不同吗?)

I installed this package.

(我安装了这个软件包。)

I read this question.

(我读了这个问题。)

I tried in different ways but no one is working.

(我尝试了不同的方法,但是没有人在工作。)

Could you please help me with some basic code or any basic tutorial ?

(您能帮我一些基本代码或任何基本教程吗?)

Here is my code

(这是我的代码)

import React, { Component } from 'react';
import Auth from '../services/Auth'
import { Router, Route, Link, IndexRoute, hashHistory, browserHistory, Redirect } from 'react-router-dom'

class Login extends Component {
    constructor(props) {
        super(props);
        this.state = {email: '',password:''};

        this.handleChange = this.handleChange.bind(this);
        this.handleClick = this.handleClick.bind(this);
    }
    handleChange (event){
        this.setState({[event.target.name]: event.target.value});
    }
    handleClick(){
        var data = {
            client_id: 2,
            client_secret: 'ispGH4SmkEeV4i5Tz9NoI0RSzid5mciG5ecw011f',
            grant_type: 'password',
            username: this.state.email,
            password: this.state.password
        }

        fetch('http://127.0.0.1:8000/oauth/token', {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(data)
        })
        .then((response) => response.json())
        .then((responseData) => {

            Auth.setToken(responseData.access_token,responseData.expires_in+ Date.now());

            this.props.history.push("/dashboard");  

        })
    }
}

export default Login;

I am getting error like below

(我收到如下错误)

在此处输入图片说明

  ask by abu abu translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Could you please show the errors?

(您能显示这些错误吗?)

The component seems incomplete to me.

(该组件对我来说似乎不完整。)

If you are using react-router v4, the component should be wrapped with the withRouter .

(如果您使用react-router v4,则应使用withRouter包装该组件 。)

This passes the history prop to the component.

(这会将历史记录属性传递给组件。)

Can you please confirm if the history prop is not undefined ?

(您能否确认history道具是否undefined ?)

Apologies if I misunderstood your question.

(抱歉我误解了您的问题。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...