Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

327
Vistas
Uncaught (in promise) Error: Invalid hook call. Hooks can only be called inside of the body of a function component

App.js:

function App() {
  PreProcess();
  const user = useSelector(selectUser);   // user initialized to null
  return user ?
    (
      <p>{user}</p>
    )
    :
    (<p>Authenticating and Authorizing</p>)
}

export default App;

preProcess.js:

export const PreProcess = async () => {
    await readEnvVars();  // read env vars in local json file
    Auth();   // need environment variables for authentication and authorization
}

auth.js

export const Auth = async () => {
    const dispatch = useDispatch();

    // some logic for authentication and authorization to get the user

    dispatch(setUser(userId));
}

Received error

Uncaught (in promise) Error: Invalid hook call. Hooks can only be called inside of the body of a function component.

at auth.js:

const dispatch = useDispatch();

Why do I get this error and how to solve this?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Hooks cannot be called inside plain javascript functions, only within another hooks, or in function components.

Reference: https://reactjs.org/docs/hooks-rules.html

about 4 years ago · Juan Pablo Isaza Denunciar

0

I solved this by moving the const dispatch = useDispatch(); up to App.js and pass the dispatch var as an argument to the method needs it.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda