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

96
Vistas
Redirecting to pages depending on conditionals in ReactJS

Given is an application with 3 pages:

  • "Mainpage"

  • "PrivatPage"

  • "UserManagementPage"

After successful login the user is redirected to a "PrivatPage". On the "PrivatPage" the user has the possibility to return to the "MainPage" or to go to the "UserManagementPage". The part with the redirecting from the "MainPage" to the "PrivatPage" works. The code looks like this:

import PublicPage from "./components/PublicPage";
import PrivatePage from "./components/PrivatePage";
import UserManagementPage from "./components/UserManagementPage";
import React, { useState, Component } from "react";
import { connect } from "react-redux";

const mapStateToProps = state => {
    return state;
};

class App extends Component {
    render() {
        const accessToken = this.props.accessToken;

        console.log(accessToken);
        let workspace;
        if (accessToken) {
            workspace = <PrivatePage />;
        } else {
            workspace = <PublicPage />;
        }

        return <div className="App">{workspace}</div>;
    }
}
export default connect(mapStateToProps)(App);

But how do I use the conditionals to get to the "UserManagementPage" ?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

if you consider functional components, you could use BrowserRouter as follows with react-router-dom. If you need to handle authentication, you can f.e. build a custom <PrivateRoute /> component and use this on your protected routes instead of <Route />. I always keep these routes in a separate file and import them in App.js. Here for demo purposes routes in App.js:

import { BrowserRouter as Router } from "react-router-dom";
// import your page components
// and add everything else you want to add to your component

const App = () => {

  return (
    <>
      <Router>
        <Routes>
          <Route path="/login" element={<Login />} />
          <Route path="/private" element={<PrivatePage />} />
          <Route path="/public" element={<PublicPage />} />
          <Route path="/user" element={<UserManagementPage />} />
        </Routes>
      </Router>
    </>
  );
};

export default App;

about 4 years ago · Juan Pablo Isaza Denunciar

0

Adding on to private blocks answer you would then in your components use the

<Redirect to='/your-route' />

You would then create a boolean state variable and once it return true you could redirect immediatley like this (where you are rendering jsx):

render() {
    {booleanState && <Redirect to='/your-route' />}
}
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