javascript - React Router navigation using history push - Stack Overflow

I am new to ReactJS. I stuck with React routing when i tried to navigate from one page to another page

I am new to ReactJS. I stuck with React routing when i tried to navigate from one page to another page history.push('/newpage') is not working for me. Login service call success i want navigate to another page.

I am using below version

"react": "^16.13.1", "react-dom": "^16.13.1", "react-router-dom": "^5.2.0",

import React, { Component } from 'react';
import { useHistory } from 'react-router-dom';
import '../Login/Login.css';
import {Auth} from '../../services/Api';
// import { Redirect } from 'react-router-dom'
class Login extends Component {

    constructor(props) {
        super(props)
        this.state = {
            userName: '',
            password: ''
        }
        this.changeEventReact = this.changeEventReact.bind(this);
        this.login = this.login.bind(this);
    }
    changeEventReact = (e) =>{
        this.setState({[e.target.name]: e.target.value});
        // console.log(this.state);
    }
    login = e=>{
        // console.log(this.state);
        Auth('login', this.state)
        .then((result)=>{
            let responseJSON = result;
            if(responseJSON.data.id != ''){
                console.log("Login success");
                const history = useHistory();
                let path = `/facilityRegister`; 
                history.push(path);
            }else{
                console.log('Login failed');
            }
        });
    }

We can use below three methods to do this

1. history.push('/facilityRegister')
2. <Redirect to='/facilityRegister'/>
3. window.location.href("/facilityRegister')

window.location.href is working as expected. But history.push and is not working.

Let me know what I missed.

I am new to ReactJS. I stuck with React routing when i tried to navigate from one page to another page history.push('/newpage') is not working for me. Login service call success i want navigate to another page.

I am using below version

"react": "^16.13.1", "react-dom": "^16.13.1", "react-router-dom": "^5.2.0",

import React, { Component } from 'react';
import { useHistory } from 'react-router-dom';
import '../Login/Login.css';
import {Auth} from '../../services/Api';
// import { Redirect } from 'react-router-dom'
class Login extends Component {

    constructor(props) {
        super(props)
        this.state = {
            userName: '',
            password: ''
        }
        this.changeEventReact = this.changeEventReact.bind(this);
        this.login = this.login.bind(this);
    }
    changeEventReact = (e) =>{
        this.setState({[e.target.name]: e.target.value});
        // console.log(this.state);
    }
    login = e=>{
        // console.log(this.state);
        Auth('login', this.state)
        .then((result)=>{
            let responseJSON = result;
            if(responseJSON.data.id != ''){
                console.log("Login success");
                const history = useHistory();
                let path = `/facilityRegister`; 
                history.push(path);
            }else{
                console.log('Login failed');
            }
        });
    }

We can use below three methods to do this

1. history.push('/facilityRegister')
2. <Redirect to='/facilityRegister'/>
3. window.location.href("/facilityRegister')

window.location.href is working as expected. But history.push and is not working.

Let me know what I missed.

Share Improve this question asked May 20, 2020 at 12:02 Harish BalakrishnanHarish Balakrishnan 1012 gold badges2 silver badges8 bronze badges 2
  • try with this.props.history.push('/path') – Arpit Vyas Commented May 20, 2020 at 12:04
  • @VyasArpit: Tried with this.props.history.push('/path'); is not working. In class ponent we can't use history – Harish Balakrishnan Commented May 24, 2020 at 4:20
Add a ment  | 

2 Answers 2

Reset to default 2

According to react faq:

You can’t use Hooks inside a class ponent

That's why const history = useHistory(); is not working here. But anyway you can't use hooks inside async functions. Use withRouter HOC instead or try to rewrite your ponent to functional.

well as say before hooks can not be use in class ponents and can only be use in function ponents like this

const app = (props) => {
     const history = useHistory();
     history.push("/path");
}

you can try this props.history.push("/path")

hope it's help :)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744266688a4565898.html

相关推荐

  • javascript - React Router navigation using history push - Stack Overflow

    I am new to ReactJS. I stuck with React routing when i tried to navigate from one page to another page

    7天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信