Skip to content

Commit

Permalink
chore: update example to react 19
Browse files Browse the repository at this point in the history
  • Loading branch information
subzero10 committed Dec 23, 2024
1 parent 64b05a4 commit 7fe8467
Show file tree
Hide file tree
Showing 7 changed files with 9,728 additions and 24,993 deletions.
34,596 changes: 9,662 additions & 24,934 deletions packages/react/example/package-lock.json

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions packages/react/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@honeybadger-io/js": "^6.10.1",
"@honeybadger-io/react": "file:..",
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^11.2.7",
"@testing-library/user-event": "^12.8.3",
"@types/jest": "^26.0.24",
"@types/node": "^12.20.25",
"@types/react": "^17.0.22",
"@types/react-dom": "^17.0.9",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"@types/react": "19.0.1",
"@types/react-dom": "19.0.2",
"react": "19.0.0",
"react-dom": "19.0.0",
"react-scripts": "5.0.1",
"typescript": "^4.4.3",
"web-vitals": "^1.1.2"
Expand Down
31 changes: 22 additions & 9 deletions packages/react/example/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
import React from 'react'
import ReactDOM from 'react-dom'
import './index.css'
import App from './App'
import reportWebVitals from './reportWebVitals'
import {Honeybadger, HoneybadgerErrorBoundary} from '@honeybadger-io/react'
import { Honeybadger, HoneybadgerErrorBoundary } from '@honeybadger-io/react'

// React < 19
// import ReactDOM from 'react-dom'

// React >= 19
import { createRoot } from 'react-dom/client'

Honeybadger.configure({
apiKey: (process.env.REACT_APP_HONEYBADGER_API_KEY || (prompt('Enter the API key for your Honeybadger project:')) as string),
environment: 'production'
apiKey: (process.env.REACT_APP_HONEYBADGER_API_KEY || (prompt('Enter the API key for your Honeybadger project:')) as string),
environment: 'production'
})

ReactDOM.render(
<HoneybadgerErrorBoundary honeybadger={Honeybadger}>
// React < 19
// ReactDOM.render(
// // @ts-expect-error "refs" is missing
// <HoneybadgerErrorBoundary honeybadger={Honeybadger}>
// <App />
// </HoneybadgerErrorBoundary>,
// document.getElementById('root')
// )

// React >= 19
const root = createRoot(document.getElementById('root')!)
root.render(<HoneybadgerErrorBoundary honeybadger={Honeybadger}>
<App />
</HoneybadgerErrorBoundary>,
document.getElementById('root')
)
</HoneybadgerErrorBoundary>)

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
Expand Down
46 changes: 17 additions & 29 deletions packages/react/src/DefaultErrorComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types';
import React from 'react'
import { DefaultErrorComponentProps } from './types';

export interface DefaultErrorComponentProps {
error: Error | null
info: React.ErrorInfo | null
}

// eslint-disable-next-line @typescript-eslint/ban-types
export default class DefaultErrorComponent extends Component<DefaultErrorComponentProps, {}> {

static propTypes = {
error: PropTypes.object,
info: PropTypes.object
}

render() {
return (
<div className='error'>
<div>
An Error Occurred
</div>
<div>
{JSON.stringify(this.props.error, null, 2)}
</div>
<div>
{this.props.info ? JSON.stringify(this.props.info, null, 2) : ''}
</div>
function DefaultErrorComponent({ error, info }: DefaultErrorComponentProps) {
return (
<div className='error'>
<div>
An Error Occurred
</div>
)
}
<div>
{JSON.stringify(error, null, 2)}
</div>
<div>
{info ? JSON.stringify(info, null, 2) : ''}
</div>
</div>
)
}

export default DefaultErrorComponent;
19 changes: 3 additions & 16 deletions packages/react/src/HoneybadgerErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import React, { Component, ComponentType, ElementType, ReactNode } from 'react'
import Honeybadger from '@honeybadger-io/js'
import DefaultErrorComponent, { DefaultErrorComponentProps } from './DefaultErrorComponent'

interface HoneybadgerErrorBoundaryProps {
honeybadger: typeof Honeybadger
showUserFeedbackFormOnError?: boolean
ErrorComponent?: ReactNode | ComponentType | ElementType
children?: ReactNode
}

interface HoneybadgerErrorBoundaryState extends DefaultErrorComponentProps {
errorOccurred: boolean
}
import React, { Component, ReactNode } from 'react'
import DefaultErrorComponent from './DefaultErrorComponent'
import { HoneybadgerErrorBoundaryProps, HoneybadgerErrorBoundaryState } from './types'

export default class HoneybadgerErrorBoundary extends Component<HoneybadgerErrorBoundaryProps, HoneybadgerErrorBoundaryState> {

Expand All @@ -26,7 +15,6 @@ export default class HoneybadgerErrorBoundary extends Component<HoneybadgerError
info: null,
errorOccurred: false
}

}

public static getDerivedStateFromError(error: Error): HoneybadgerErrorBoundaryState {
Expand All @@ -42,7 +30,6 @@ export default class HoneybadgerErrorBoundary extends Component<HoneybadgerError
}

componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
this.setState({ errorOccurred: true, error: error, info: errorInfo })
this.props.honeybadger.notify(error, { context: errorInfo as never })
}

Expand Down
17 changes: 17 additions & 0 deletions packages/react/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ComponentType, ElementType, ErrorInfo, PropsWithChildren, ReactNode } from 'react';
import Honeybadger from '@honeybadger-io/js';

export type DefaultErrorComponentProps = {
error: Error | null
info: ErrorInfo | null
}

export type HoneybadgerErrorBoundaryProps = PropsWithChildren<{
honeybadger: typeof Honeybadger
showUserFeedbackFormOnError?: boolean
ErrorComponent?: ReactNode | ComponentType | ElementType
}>

export type HoneybadgerErrorBoundaryState = {
errorOccurred: boolean
} & DefaultErrorComponentProps;
3 changes: 2 additions & 1 deletion packages/react/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"node_modules"
],
"include": [
"./src/*.tsx"
"./src/*.tsx",
"./src/*.ts"
],
"references": [
{
Expand Down

0 comments on commit 7fe8467

Please sign in to comment.