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

92
Views
Can not catch errors with React/Redux

I have a login error handling with redux which looks likes this:

export const login = (params: any) => async (dispatch: Dispatch) => {
  try {
    const authData = await API.post("login", params);
    sessionStorage.setItem("access_token", authData?.data?.access_token);
    dispatch(ActionCreators.getTokens(authData?.data?.access_token));
  } catch (error) {
    throw error;
  }
};

Then in my component, I am trying to catch the error with try/catch again, but I do not have an access to response. Guess because of try/catch scope.

  const submitHandler = async (e: SyntheticEvent) => {
    e.preventDefault();
    try {
      var res: any = await dispatch(
        login({
          email,
          password,
          token,
        })
      );

      router.push("/dashboard");
    } catch (error) {
      setError(res?.response.data.message);
    }
  };

Any idea how to fix that? Thank you.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I had hit same problem and was also catching error in both of my react component and redux component file. The compiler does not work like this, i removed the catch block from my component file and it was worked. Please try it, thanks

about 4 years ago · Juan Pablo Isaza Report

0

It is unnecessary to throw the error in catch block of redux action. Removing it works. Catch block in your component file is enough to handle error.

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!