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

155
Views
Issue with history.push

I'm working on react Disney+ clone etc and I was trying to do something like: if user isnt authorized then show login page but if authorized then show content. I used useHistory for this. And it works for a second, it just starts to download login page (background image is loading, but text of login page is visible) and then it disappears and content page is shown. Url changes for a second too.

App.js

function App() {
  
  return (
    <div className="App">
      <Router>
        <Header />
        <Switch>
        <Route path="/login"> 
              <Login/>
          </Route>
          <Route path="/detail/:id"> 
              <Detail/>
          </Route>
          <Route path="/"> 
            <Home/>
          </Route>
        </Switch>
      </Router>    
    </div>
  );
}

Header.js

import React from 'react'

import {selectUserName, selectUserPhoto, setUserLogin, setUserSignOut} from '../../features/user/userSlice' ;
import { useDispatch, useSelector } from "react-redux" ;
import  { auth, provider} from "../../firebase"
import { useHistory} from 'react-router-dom';


const Header = () => {

    const dispatch = useDispatch()
    const userName = useSelector(selectUserName);
    const userPhoto = useSelector(selectUserPhoto);
    
    const history = useHistory();
    
    const signIn = () => {
        auth.signInWithPopup(provider)
        .then((result) => {
            let user = result.user;
            dispatch(setUserLogin({
                name: user.displayName,
                email: user.email,
                photo: user.photoURL
            }))
            history.push('/');
        })
    }
    const signOut = () => {
        auth.signOut()
        .then(()  => {
            
            dispatch(setUserSignOut());    
            history.push('/login');           
        })
    }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Based on your issue you can handle Routes in a different way. So you have routes which can only shown during unauthorised situation and some routes only shown for authorised user. For that you can have following implementation.

First you can create ProtectedRoute function.

import React from "react";
import { Redirect, Route } from "react-router-dom";

function ProtectedRoute({ component: Component, ...restOfProps }) {
  const isAuthenticated = localStorage.getItem("isAuthenticated");
  console.log("this", isAuthenticated);

  return (
    <Route
      {...restOfProps}
      render={(props) =>
        isAuthenticated ? <Component {...props} /> : <Redirect to="/login" />
      }
    />
  );
}

export default ProtectedRoute;

And then you can use this function in your main App where you will declare your routes with component.

import ProtectedRoute from "./component/ProtectedRoute";

function App() {
  return (
    <div className="App">
      <BrowserRouter>
        <Route path="/login" component={Login} />
        <ProtectedRoute path="/protected" component={ProtectedComponent} />
      </BrowserRouter>
    </div>
  );
}
export default App;
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!