Recovering from Errors
The rescues
lifecycle callbacks run when an error is raised from within or below a component.
At the end of the rescue the component tree will be completely re-rendered from scratch. In its simplest form we need nothing but an empty block.
When an error occurs it will be caught by the rescues block, and App
and all lower components will be re-generated (not just re-rendered).
In most cases you may want to warn the user that something is going wrong, and also record some data about the event:
If you don't want to involve the user, then be careful: To prevent infinite loops the React engine will not rescue failures occurring during the re-generation of the component tree. If not involving the user you may want to consider how to insure that system state is completely reset in the rescue.
The rescues method can also take explicit Error types to be rescued:
Like other lifecycle methods you can have multiple rescues in the same component:
Like Ruby's rescue keyword, errors will be caught by the innermost component with a rescues
callback that handles that error.
The data passed to the rescue handler is an array of two items, the Ruby error that was thrown, and details generated by the React engine.
Caveats
You cannot rescue errors raised in lifecycle handlers in the same component. Errors raised by lifecycle handlers in inner components are fine, just not in the same component as the rescue.
Errors raised in event handlers will neither stop the rendering cycle, nor will they be caught by a rescue callback.
Last updated