Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
Link to(react-router-dom) conditional rendering when a button is clicked

If you type user in the text box, it will go to the user page, and if you type admin, it will go to the admin page.

This is my code.

constructor(props) {
    super(props);
    this.state = {
        userType : 0
    };
    this.handleLogin = this.handleLogin.bind(this);
}

If the userType is 0, it tries to go to the user page, and if it is 1, it tries to go to the admin page.

 handleLogin() {
    if(this.state.id==='admin'){
        this.setState({userType : 1});
    }
    else{
        this.setState({userType : 0});
    }
}

I changed the userType to 1 when admin was entered. I have confirmed that it works well until this operation.

<Link to={this.state.userType === 0 ? '/userPage' : '/adminPage'}>
      <button className='loginButton' onClick={this.handleLogin}>Login</button>
</Link>

The problem seems to be this part. But I don't know how to fix it. Please, give me a solution.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You should not use the Link and a button at the same time. You can simply do like this :

<Link to={this.state.id==='admin' ? '/adminPage' : '/userPage'}>
      Login
</Link>
about 4 years ago · Juan Pablo Isaza Report

0

If you want to redirect user to another page on an onClick event, then histoty.push() would the right way to do it. The declarative routing should be used here. You can implement this way-

    handleLogin() {
        if(this.state.id==='admin'){
            this.props.history.push("/adminPage");
        }
        else{
            this.props.history.push("/userPage");
        }
    }

<button className='loginButton' onClick={this.handleLogin}>Login</button>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!