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

136
Visualizações
Fire a Redux Selector only when exists

I have a redux state setup for my map which looks like the following. https://gyazo.com/3f9d6b095b252dcd29146cf1e9254908 It works great a user is logged in and is perfect, however, it does not work currently from a cold start where you have to log in. This is because in my App.jsx, I am instantly using the selector to select the user login AND also the userID.

const App = () => {
  const user = useSelector(state=>state.user.currentUser)
  const userID = useSelector(state=>state.user.currentUser._id)

  console.log(user)

  return (
  // react-router-v5
  <Router>
      <Switch>
        <Route exact path="/">
          <Landing/>
        </Route>
        <Route exact path="/login">
          { user ? <Redirect to="/"/> : <LoginStandard/>}
        </Route>

        {user && userID &&
        <Route path={`/userprofile/${userID}`}>
          <UserProfile/>
          This is the unique user!
        </Route>
        }

      </Switch>
    </Router>
  )
};

export default App;

My issue is that although the user works fine if there's no user, userID will throw an error as below:

TypeError: Cannot read properties of null (reading '_id')

How do I do this so that the userID is only fired when the user exists? I can't call this outside the top level.

Furthermore, how would you recommend passing this const user/ const userID throughout my app? Through props?

about 4 years ago · Juan Pablo Isaza
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