-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add error boundaries to smart components
- Loading branch information
Showing
6 changed files
with
137 additions
and
3 deletions.
There are no files selected for viewing
3 changes: 2 additions & 1 deletion
3
starters/nextjs-starter-approuter-ts/components/smart-components/client-components.ts
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
45 changes: 45 additions & 0 deletions
45
starters/nextjs-starter-approuter-ts/components/smart-components/error-boundary.tsx
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React, { Component, Suspense } from "react"; | ||
|
||
interface Props { | ||
children: React.ReactNode; | ||
} | ||
|
||
export class SmartComponentErrorBoundary extends Component<Props> { | ||
state = { | ||
hasError: false, | ||
}; | ||
|
||
static getDerivedStateFromError() { | ||
return { hasError: true }; | ||
} | ||
|
||
render() { | ||
if (this.state.hasError) { | ||
return ( | ||
<div className="my-2 text-gray-400"> | ||
Something went wrong while rendering this smart component. | ||
</div> | ||
); | ||
} | ||
|
||
return this.props.children; | ||
} | ||
} | ||
|
||
const SmartComponentSuspenseErrorBoundary = ({ children }: Props) => { | ||
return ( | ||
<SmartComponentErrorBoundary> | ||
<Suspense fallback={<div>Loading...</div>}>{children}</Suspense> | ||
</SmartComponentErrorBoundary> | ||
); | ||
}; | ||
|
||
export const withSmartComponentErrorBoundary = | ||
// eslint-disable-next-line react/display-name | ||
(Component: React.ComponentType) => (props: Record<string, unknown>) => ( | ||
<SmartComponentSuspenseErrorBoundary> | ||
<Component {...props} /> | ||
</SmartComponentSuspenseErrorBoundary> | ||
); | ||
|
||
export default SmartComponentErrorBoundary; |
45 changes: 45 additions & 0 deletions
45
starters/nextjs-starter-ts/components/smart-components/error-boundary.tsx
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React, { Component, Suspense } from "react"; | ||
|
||
interface Props { | ||
children: React.ReactNode; | ||
} | ||
|
||
export class SmartComponentErrorBoundary extends Component<Props> { | ||
state = { | ||
hasError: false, | ||
}; | ||
|
||
static getDerivedStateFromError() { | ||
return { hasError: true }; | ||
} | ||
|
||
render() { | ||
if (this.state.hasError) { | ||
return ( | ||
<div className="my-2 text-gray-400"> | ||
Something went wrong while rendering this smart component. | ||
</div> | ||
); | ||
} | ||
|
||
return this.props.children; | ||
} | ||
} | ||
|
||
const SmartComponentSuspenseErrorBoundary = ({ children }: Props) => { | ||
return ( | ||
<SmartComponentErrorBoundary> | ||
<Suspense fallback={<div>Loading...</div>}>{children}</Suspense> | ||
</SmartComponentErrorBoundary> | ||
); | ||
}; | ||
|
||
export const withSmartComponentErrorBoundary = | ||
// eslint-disable-next-line react/display-name | ||
(Component: React.ComponentType) => (props: Record<string, unknown>) => ( | ||
<SmartComponentSuspenseErrorBoundary> | ||
<Component {...props} /> | ||
</SmartComponentSuspenseErrorBoundary> | ||
); | ||
|
||
export default SmartComponentErrorBoundary; |
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
41 changes: 41 additions & 0 deletions
41
starters/nextjs-starter/components/smart-components/error-boundary.jsx
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React, { Component, Suspense } from "react"; | ||
|
||
export class SmartComponentErrorBoundary extends Component { | ||
state = { | ||
hasError: false, | ||
}; | ||
|
||
static getDerivedStateFromError() { | ||
return { hasError: true }; | ||
} | ||
|
||
render() { | ||
if (this.state.hasError) { | ||
return ( | ||
<div className="my-2 text-gray-400"> | ||
Something went wrong while rendering this smart component. | ||
</div> | ||
); | ||
} | ||
|
||
return this.props.children; | ||
} | ||
} | ||
|
||
const SmartComponentSuspenseErrorBoundary = ({ children }) => { | ||
return ( | ||
<SmartComponentErrorBoundary> | ||
<Suspense fallback={<div>Loading...</div>}>{children}</Suspense> | ||
</SmartComponentErrorBoundary> | ||
); | ||
}; | ||
|
||
export const withSmartComponentErrorBoundary = | ||
// eslint-disable-next-line react/display-name | ||
(Component) => (props) => ( | ||
<SmartComponentSuspenseErrorBoundary> | ||
<Component {...props} /> | ||
</SmartComponentSuspenseErrorBoundary> | ||
); | ||
|
||
export default SmartComponentErrorBoundary; |
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