Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

161
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda