Do you want to request a feature or report a bug?
Feature idea
What is the current behavior?
Currently there is no hook for dealing with errors in a component's sub-tree.
What is the expected behavior?
Have a hook that allows for functional components to act as Error Boundaries.
Example:
function myErrorBoundary() {
const caughtError = useErrorCatching();
if (caughtError !== null) { return <ErrorHandler error={caughtError} />; }
return <RegularContent />;
}
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
n/a
This is something the React team is working on.
See the last bullet point in How do lifecycle methods correspond to Hooks?
componentDidCatch and getDerivedStateFromError: There are no Hook equivalents for these methods yet, but they will be added soon.
Do you have a roadmap(or abstract date) for when we're use it? :D
No specific date, you'll have to use an ErrorBoundary
class for now.
(Also, we don't think useError
is the right design for this.)
Thanks Dan,
Could you point us to some discussion about why useError
would not be the right design?
(guessing it's because it forces the user to handle the error through a conditional)
For the same reason <Provider>
or memo()
are not Hooks. They have an explicit meaning in the tree structure (i.e. their position in the tree affects for how errors/bailouts/context propagate up or down). You don't want a random third-party Hook to be able to "hide" useError()
somewhere in it without seeing the result reflected in the tree structure.
Ah, totally makes sense. So, might we except a solution similar to forward refs? e.g. const MyErrorHandler = React.errorBoundary((props, error) => {...})
Possibly although we also had other thoughts related to unifying this API with multipass rendering (previously known as call/return experiment).
I also wrote this on the topic: https://overreacted.io/why-isnt-x-a-hook/
I think we can close since it's not directly actionable in the current form.
Any other issue we can track for this? Interested in how this develops
@gaearon how do you propose to use hooks with error boundaries please, with the error boundaries now the hooks error is uncatchable
@gaeron I don't want write something like 芦+1禄 for previous question but it is still actual and not only for me but for all devs who will find that thread
Writing at this epic thread issue in almost 2 years since it's started. The problem is still exists :(
Most helpful comment
@gaeron I don't want write something like 芦+1禄 for previous question but it is still actual and not only for me but for all devs who will find that thread