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

324
Views
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 answers
Answer question

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 Report

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 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!