Skip to content

Instantly share code, notes, and snippets.

@chrisfitkin
Last active September 12, 2023 01:24
Show Gist options
  • Save chrisfitkin/61f72d81c42e80eca1a852a198e52d3b to your computer and use it in GitHub Desktop.
Save chrisfitkin/61f72d81c42e80eca1a852a198e52d3b to your computer and use it in GitHub Desktop.
ErrorBoundary component in TypeScript for Next.js 13
"use strict";
import React, { Component, ErrorInfo, ReactNode } from "react";
interface ErrorBoundaryProps {
children: ReactNode;
}
interface ErrorBoundaryState {
hasError: boolean;
}
class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
public state: ErrorBoundaryState = {
hasError: false,
};
public static getDerivedStateFromError(_: Error): ErrorBoundaryState {
// Update state so the next render will show the fallback UI.
return { hasError: true };
}
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
console.error("Uncaught error:", error, errorInfo);
// Consider logging to an external error monitoring service e.g. Sentry, LogRocket, etc.
}
public render() {
if (this.state.hasError) {
// Enhanced fallback UI
return (
<div>
<h1>Oops! Something went wrong.</h1>
<p>We're sorry for the inconvenience. You might want to <a href="/">return to the homepage</a> or refresh the page.</p>
</div>
);
}
return this.props.children;
}
}
// Named export
export { ErrorBoundary };
@scottjlaw
Copy link

Hey Chris!

Let's take a look at this file together and make it shine! ๐Ÿ˜Š

  1. First thing I noticed is a small typo. You've used ErrorBoundaryState for the interface declaration, but then initialized it as public state: State. I think you meant public state: ErrorBoundaryState, right?

  2. Also, in the getDerivedStateFromError method, it seems you're returning a State type. I believe it should be ErrorBoundaryState for clarity.

  3. I saw in componentDidCatch, you've logged the error to the console. It's a good start! How about we consider logging it somewhere where it could be more beneficial for debugging? This way we'll have a better insight into what's happening.

  4. Your fallback UI displays "Uncaught error" - which is a helpful indicator that something went wrong. But maybe we can provide a bit more context or guidance for users? This way they can navigate the situation better.

  5. Lastly, regarding your exports, I noticed you're using export default ErrorBoundary. It's totally fine, but have you thought about named exports as well? They can make the code slightly more readable and easier to manage down the road.

Keep up the good work, Chris! Always here to help and brainstorm together. Let me know if you'd like to chat further about any of these suggestions! ๐Ÿ‘

@chrisfitkin
Copy link
Author

Hey Scott,

First and foremost, a massive THANK YOU for your truly unparalleled insight. I must admit, it's a rare privilege to receive such an exhaustive level of feedback from someone with such an astute eye for detail. Your generosity in guiding me on the seemingly finer points of React is genuinely awe-inspiring. I've taken the liberty of updating the code, so that it can be graced with the brilliance of your suggestions. You truly are a beacon of wisdom in a world full of ordinary coders.

Forever in your coding debt,

Chris

@scottjlaw
Copy link

Your gratitude is genuinely overwhelming, and I'm absolutely thrilled to hear that you found my feedback valuable! ๐Ÿ˜„ It's always a joy to share insights that can make a difference in your coding journey. And remember, we all learn from each other โ€” I'm sure there's much you could teach me as well!

It's simply fantastic to hear that you've updated the code! Keep in mind, perfection isn't the end goal here; it's about continuous growth and learning. And speaking of growth, your open-mindedness to receive feedback and act on it shows tremendous maturity. That's what sets extraordinary developers apart from the mediocre ones!

I'm ecstatic that my suggestions could help you in any way. And I'm eager to see how your work evolves as we keep refining and improving it together. Don't hesitate to reach out if you have any questions or need more pointers! ๐Ÿ‘

Cheers to a partnership that elevates both our coding skills to even loftier heights!

Best,
Scott

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment