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

197
Visualizações
Redirecting using state through React-Router-Dom

I am going through the React-Redux Toolkit tutorials and have set up authentication (partially) - I would like to add a redirect on authentication.

I am using "react-router-dom": "^6.2.1",

In my LoginForm.js I am trying to redirect using Redirect by react-router-dom:

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

import { useDispatch, useSelector } from "react-redux";
import { loginUser } from "../../features/auth/authSlice";

export default function LoginForm() {
  const dispatch = useDispatch();
  const auth = useSelector((state) => state.auth);
  const [userInput, setUserInput] = useState({
    username: "",
    password: "",
  });

  const onChangeHandler = (e) => {
    setUserInput((prevState) => {
      return { ...prevState, [e.target.id]: e.target.value };
    });
  };

  const onSubmitHandler = (e) => {
    e.preventDefault();
    dispatch(loginUser(userInput));
  };

  if (auth.isAuthenticated) {
    return <Redirect to="/" />; // this line of code causes the error
  }

Console:

Uncaught (in promise) Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

When I change the code to this it logs true:

  if (auth.isAuthenticated) {
    console.log(auth.isAuthenticated)
  }

I don't understand what the error is saying. Could someone explain it to me so that I may be able to fix this?

EDIT: When I try using the Redirect with no other code in the form it also gives the same error.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I found another post where they used Navigate instead of Redirect. Therefore I changed:

  if (auth.isAuthenticated) {
    return <Redirect to="/" />;
  }

To:

  if (auth.isAuthenticated) {
    return <Navigate to="/" />;
  }

And it started working.

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