-
Hi everyone! I am not quite sure if i understand how the Example here: Do i need to handle this kind of errors on my own? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Yeah, inside a click handler is outside of the rendering flow. ErrorBoundaries handle issues with rendering, or reactive propagation, or view code. Throwing an error in an event handler there is no hierarchy there anymore as it is just attached to the DOM. There might be some ways to push that context in, but it isn't there by default right now. |
Beta Was this translation helpful? Give feedback.
-
I also had the problem of wanting an Error Boundary to handle an error outside the rendering flow, so the user knows something went wrong and the app doesn't continue with a corrupted state. I managed to solve it by creating a custom hook Here are the relevant parts of my code as an example, maybe it helps someone with the same problem: function useAuthStateChangeListener(listenerFunc: AuthStateChangeListener) {
const throwErrorsToErrorBoundary = useThrowErrorsToErrorBoundary();
onMount(() => {
FirebaseAuthentication.addListener("authStateChange", change => {
throwErrorsToErrorBoundary(() => {
listenerFunc(change); // this may throw, but it's outside render
});
});
});
}
function useThrowErrorsToErrorBoundary(): (fnThatMayThrow: () => void) => void {
const owner = getOwner();
return function throwErrorsToErrorBoundary(fnThatMayThrow: () => void): void {
runWithOwner(owner, fnThatMayThrow);
};
} |
Beta Was this translation helpful? Give feedback.
Yeah, inside a click handler is outside of the rendering flow. ErrorBoundaries handle issues with rendering, or reactive propagation, or view code. Throwing an error in an event handler there is no hierarchy there anymore as it is just attached to the DOM. There might be some ways to push that context in, but it isn't there by default right now.