Skip to content

Commit d06bf63

Browse files
author
Joschua Schneider
committed
Implements HOC to wrap ErrorBoundary in typescript
1 parent 950ef56 commit d06bf63

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/create-error-boundary.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import React from "react"
2+
3+
import {
4+
ErrorBoundary,
5+
ErrorBoundaryProps,
6+
OnDidCatchCallback,
7+
} from "./ErrorBoundary"
8+
9+
/**
10+
* createErrorBoundary
11+
* Accepts a onDidCatch callback.
12+
* Creates a UseErrorBoundaryWrapper HOC, to keep the onDidCatch callback while
13+
* still providing the ability to pass props to the ErrorBoundary
14+
*/
15+
16+
export type UseErrorBoundaryWrapper = (
17+
props: Omit<ErrorBoundaryProps, "onDidCatch">
18+
) => React.ReactElement
19+
20+
export function createErrorBoundary(
21+
onDidCatch: OnDidCatchCallback
22+
): UseErrorBoundaryWrapper {
23+
// Return function component that wraps ErrorBoundary and passes props to it
24+
return function UseErrorBoundaryWrapper(props) {
25+
// Return ErrorBoundary with original onDidCatch and the current props
26+
return React.createElement<ErrorBoundaryProps>(ErrorBoundary, {
27+
onDidCatch,
28+
children: props.children,
29+
render: props.render,
30+
renderError: props.renderError,
31+
})
32+
}
33+
}

0 commit comments

Comments
 (0)