Skip to content

Commit

Permalink
use error handling hook instead of state workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
macfarlandian committed Nov 5, 2020
1 parent 576be9f commit f78a420
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions spotlight-client/src/AuthWall/AuthWall.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
// =============================================================================

import { observer } from "mobx-react-lite";
import React, { useEffect, useState } from "react";
import { withErrorBoundary } from "react-error-boundary";
import React, { useEffect } from "react";
import { useErrorHandler, withErrorBoundary } from "react-error-boundary";
import { ERROR_MESSAGES } from "../constants";
import ErrorMessage from "../ErrorMessage";
import Loading from "../Loading";
Expand All @@ -29,21 +29,17 @@ import VerificationRequired from "../VerificationRequired";
*/
const AuthWall: React.FC = ({ children }) => {
const { userStore } = useRootStore();
const [effectError, setEffectError] = useState<undefined | Error>();
const handleError = useErrorHandler();

useEffect(() => {
if (!userStore.isAuthorized) {
userStore.authorize().then((result) => {
if (result instanceof Error) {
setEffectError(result);
handleError(result);
}
});
}
}, [userStore]);

if (effectError) {
throw effectError;
}
}, [handleError, userStore]);

if (userStore.isLoading) {
return <Loading />;
Expand Down

0 comments on commit f78a420

Please sign in to comment.