Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

converted ErrorBoundary to functional component #404

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Changelog format is loosely based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.1.0

### Changed

- ErrorBoundary to functional component

## 1.0.0

### Added
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fhir-react",
"version": "1.0.0",
"version": "1.1.0",
"description": "React component library for displaying FHIR Resources ",
"main": "build/index.js",
"peerDependencies": {
Expand Down
46 changes: 24 additions & 22 deletions src/components/containers/FhirResource/FhirResource.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
import React from 'react';
import React, { useState } from 'react';
import * as FhirResourceTypes from '../../supportedFhirResourceList';
import ResourceContainer from '../ResourceContainer';
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false, error: null };
}

componentDidCatch(error, info) {
// Display fallback UI
this.setState({ hasError: true, error });
// You can also log the error to an error reporting service
// logErrorToMyService(error, info);
console.error(
'An error occured when trying to render a FHIR Component:',
error,
);
}
const ErrorBoundary = props => {
const [hasError, setHasError] = useState(false);
const [error, setError] = useState();

render() {
if (this.state.hasError) {
const errCheck = () => {
if (hasError) {
// You can render any custom fallback UI
return (
<ResourceContainer {...this.props}>
<FhirResourceTypes.Generic {...this.props} />
<ResourceContainer>
<FhirResourceTypes.Generic />
</ResourceContainer>
);
}
return this.props.children;
return props.children;
};

try {
const content = errCheck();
return content;
} catch (err) {
setError(err);
setHasError(true);
console.error(
'An error occured when trying to render a FHIR Component:',
error,
);
const content = errCheck();
return content;
}
}
};

class FhirResource extends React.Component {
renderSwitch() {
Expand Down