Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //class ErrorBoundary
- import React from 'react';
- import PropTypes from 'prop-types';
- class ErrorBoundary extends React.Component {
- constructor(props) {
- super(props);
- this.state = { error: null, errorInfo: null };
- }
- componentDidCatch(error, errorInfo) {
- this.setState({
- error,
- errorInfo,
- });
- }
- render() {
- if (this.state.errorInfo) {
- return (
- <div>
- something here
- </div>
- );
- }
- return this.props.children;
- }
- }
- ErrorBoundary.propTypes = {
- children: PropTypes.node,
- };
- export default ErrorBoundary;
- // class ErrorClass not working
- import React from 'react';
- import PropTypes from 'prop-types';
- class ErrorClass extends React.Component {
- constructor(props) {
- super(props);
- this.renderSomethingSpecific = this.renderSomethingSpecific.bind(this);
- }
- renderSomethingSpecific(prop) {
- throw new Error('test');
- }
- render() {
- return (
- <div>
- <div>Some text to always display</div>
- <ErrorBoundary>
- {this.renderSomethingSpecific();}
- </ErrorBoundary>
- </div>
- )
- }
- ErrorBoundary.propTypes = {
- children: PropTypes.node,
- };
- export default ErrorBoundary;
- // class ErrorClass which is working
- import React from 'react';
- import PropTypes from 'prop-types';
- class ErrorClass extends React.Component {
- constructor(props) {
- super(props);
- this.renderSomethingSpecific = this.renderSomethingSpecific.bind(this);
- }
- render() {
- return (
- <div>
- <div>Some text to always display</div>
- <ErrorBoundary>
- {<SpecificComponentWithError />}
- </ErrorBoundary>
- </div>
- )
- }
- ErrorBoundary.propTypes = {
- children: PropTypes.node,
- };
- export default ErrorBoundary;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement