Created
August 25, 2020 09:08
-
-
Save rgabaydullov/35151cd9be3eecbf0658a84fc4e8600d to your computer and use it in GitHub Desktop.
React error handling example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
/* обязательно ознакомьтесь с документацией https://ru.reactjs.org/docs/error-boundaries.html */ | |
/* к сожалению сейчас hooks не поддерживают работу с методом React Lifecycle API: componentDidCatch */ | |
export const withErrorBoundaries = (Wrapped) => { | |
class ErrorBoundary extends React.PureComponent { | |
/* укажите уникальное название компонента, для упрощения отладки в DevTools */ | |
static displayName = `ComponentWithErrorBoundary(${Wrapped.name || 'Wrapped'})`; | |
componentDidCatch(error, errorInfo) { | |
middlewareErrorBoundary(error, errorInfo); // ваш уникальный middleware для сбора и обработки ошибок | |
} | |
renderComponent = (context) => <Wrapped {...this.props} {...context} />; | |
render() { | |
return ( | |
<Consumer> | |
{/* передаем функцию без вызова по гайду */} | |
{this.renderComponent} | |
</Consumer> | |
); | |
} | |
} | |
return ErrorBoundary; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment