Last active
June 18, 2020 23:53
-
-
Save toruticas/ec7562274b152572cf84aa9ba37ff102 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Suspense, ReactNode } from 'react' | |
import { cache } from 'swr' | |
import { GITHUB_PROFILE_API } from './api' | |
import { ProfileData } from './ProfileData' | |
import { SkeletonLoading } from './SkeletonLoading' | |
import { ConfusedIcon } from './ConfusedIcon' | |
import './ErrorBoundary.css' | |
interface State { | |
hasError: boolean | |
} | |
class ErrorBoundary extends React.Component { | |
state = { hasError: false } | |
static getDerivedStateFromError(): State { | |
return { hasError: true } | |
} | |
handleClick = (): void => { | |
cache.delete(`err@${GITHUB_PROFILE_API}`) | |
this.setState({ hasError: false }) | |
} | |
render(): ReactNode { | |
const { hasError } = this.state | |
const { children } = this.props | |
if (!hasError) { | |
return children | |
} | |
return ( | |
<div className="ProfileErrorBoundary" onClick={this.handleClick}> | |
<div className="info"> | |
<ConfusedIcon /> | |
<p>Something Wen't Wrong</p> | |
<p>Click to retry</p> | |
</div> | |
<SkeletonLoading /> | |
</div> | |
) | |
} | |
} | |
export { ErrorBoundary } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ProfileContainer: FC = () => ( | |
<ErrorBoundary> | |
<Suspense fallback={<SkeletonLoading />}> | |
<ProfileData /> | |
</Suspense> | |
</ErrorBoundary> | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment