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

208
Visualizações
Can't add Authenticated Routes to react router dom

I currently use react-router-dom to separate routing for authenticated/non-authenticated people. But Element has an error for missing the properties.

How can withoutAuth() work for authentication routes?

Type '() => Element' is missing the following properties from type 'ReactElement<any, string | JSXElementConstructor>': type, props, key

function App() {
  return (
    <BrowserRouter>
      <AuthProvider>
        <Routes>
          <Route path="/dashboard" element={<Dashboard />} />
          <Route path="/" element={withoutAuth(<Signup />)} />
        </Routes>
      </AuthProvider>
    </BrowserRouter>
  );
}

const withoutAuth = (Element: ElementType) =>
  function WithoutAuth() {
    const { currentFBUser } = useAuthContext()
    if (currentFBUser) {
      return <Navigate to="/dashboard" />
    }
    return <Element />
  }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You don't generally pass JSX literals to Higher Order Components, and the element prop takes a JSX literal instead of a reference to a React component.

// (1) Create a new decorated component
const SignUpWithoutAuth = withoutAuth(Signup);

function App() {
  return (
    <BrowserRouter>
      <AuthProvider>
        <Routes>
          <Route path="/dashboard" element={<Dashboard />} />
          // (2) Pass the new decorated component as JSX to `element` prop
          <Route path="/" element={<SignUpWithoutAuth />} />
        </Routes>
      </AuthProvider>
    </BrowserRouter>
  );
}

This being said, it would be more conventional to implement as a wrapper component than a HOC in RRDv6.

Example:

function WithoutAuth({ children }) {
  const { currentFBUser } = useAuthContext()
  if (currentFBUser) {
    return <Navigate to="/dashboard" />
  }
  return children;
}

function App() {
  return (
    <BrowserRouter>
      <AuthProvider>
        <Routes>
          <Route path="/dashboard" element={<Dashboard />} />
          <Route
            path="/"
            element={(
              <WithoutAuth>
                <Signup />
              </WithoutAuth>
            )}
          />
        </Routes>
      </AuthProvider>
    </BrowserRouter>
  );
}

Either should work.

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