Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

102
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda