-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suspending to opt-out of SSR keeps logging uncaught errors #6106
Comments
After a bit of digging into React's source code it looks like all the recoverable errors are eventually logged: https://github.com/facebook/react/blob/d1c8cdae3b20a670ee91b684e8e0ad0c400ae51c/packages/react-reconciler/src/ReactFiberWorkLoop.js#L2897-L2909 Still not sure how to handle this though. I'd love to not have console errors for this specific case. |
Hi @iamkd
It seems that you are expecting the
As the documentation mentions when an error occurs within a component, React will propagate the error to the nearest Error Boundary component.
Error BoundaryError boundaries are special class components that define static methods class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(error) {
// Update state to indicate that an error has occurred
return { hasError: true };
}
componentDidCatch(error, errorInfo) {
// Log or handle the error here
console.error(error);
// You can also send the error to monitoring tools if needed
// handleMonitoring(error, errorInfo.componentStack);
// TODO
}
render() {
if (this.state.hasError) {
// You can render any custom fallback UI
return this.props.fallback;
}
return this.props.children;
}
} Then you can wrap a part of your component tree with it: <ErrorBoundary fallback={<p>Something went wrong</p>}>
<Profile />
</ErrorBoundary> The legacy documentation says::
Hopefully this makes sense |
Hi @A7med3bdulBaset! This absolutely makes sense, however the specific section I was referring to is saying:
Which means that in the case of an example component: function Chat() {
if (typeof window === "undefined") {
throw Error("Skip rendering on server please");
}
return <div>chat</div>
} and it being used like <Suspense fallback={<div>Placeholder</div>}><Chat /></Suspense> will return a placeholder in SSR'd HTML and then successfully render the chat div. It will never reach any |
When the docs say "React will not display the error to the user" it means the error boundary / UI. For console reporting, when React recovers from an error, and no If you want to silence these errors from the console in Remix, you can provide a onRecoverableError to the hydrateRoot(
document,
<StrictMode>
<RemixBrowser />
</StrictMode>,
{
onRecoverableError: (error) => {
// Ignore known errors.
if (error.message === 'Skip rendering on server please') {
return;
}
// Passthrough unknown errors, or report to your error logging service.
console.error('React recovered from an error:', error);
}
}
); |
I've been trying to build a client-only component as described in this section:
https://react.dev/reference/react/Suspense#providing-a-fallback-for-server-errors-and-server-only-content
I am using Remix with streaming APIs and it works as expected, but my custom thrown error is getting logged in the console as "Uncaught error: ...", even though it's caught by Suspense. While this does not affect the behavior, it is getting into our monitoring tools and basically looks like something that shouldn't happen.
I'm creating an issue here just to make sure I am not wrong and the docs are not wrong before considering it a bug in Remix or React. Thanks!
The text was updated successfully, but these errors were encountered: