Skip to content

Commit 4368fc0

Browse files
Update withErrorBoundary types for forwards compat (#211)
Co-authored-by: Brian Vaughn <[email protected]>
1 parent e1a6a0a commit 4368fc0

File tree

4 files changed

+43
-25
lines changed

4 files changed

+43
-25
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"@preconstruct/cli": "^2.8.1",
6161
"@types/assert": "^1.5.10",
6262
"@types/jest": "^26.0.15",
63-
"@types/react": "^18",
63+
"@types/react": "^18.3.17",
6464
"@types/react-dom": "^18",
6565
"assert": "^2.0.0",
6666
"eslint": "^9.13.0",

pnpm-lock.yaml

Lines changed: 6 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/withErrorBoundary.test.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @jest-environment jsdom
33
*/
44

5-
import { Component, createRef, PropsWithChildren } from "react";
5+
import { Component, createRef, forwardRef, PropsWithChildren } from "react";
66
import { createRoot } from "react-dom/client";
77
import { act } from "react-dom/test-utils";
88
import { withErrorBoundary } from "./withErrorBoundary";
@@ -81,4 +81,26 @@ describe("withErrorBoundary", () => {
8181
expect(ref.current).not.toBeNull();
8282
expect(typeof ref.current?.test).toBe("function");
8383
});
84+
85+
it("should forward dom refs", () => {
86+
type Props = { foo: string };
87+
88+
const Div = forwardRef<HTMLDivElement, Props>((props, ref) => {
89+
return <div ref={ref}>{props.foo}</div>;
90+
});
91+
Div.displayName = "Div";
92+
93+
const Wrapped = withErrorBoundary(Div, {
94+
fallback: <div>Error</div>,
95+
});
96+
97+
const ref = createRef<HTMLDivElement>();
98+
99+
act(() => {
100+
root.render(<Wrapped foo="abc" ref={ref} />);
101+
});
102+
103+
expect(ref.current).not.toBeNull();
104+
expect(ref.current).toBeInstanceOf(HTMLDivElement);
105+
});
84106
});

src/withErrorBoundary.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
import {
22
createElement,
33
forwardRef,
4-
ForwardedRef,
54
RefAttributes,
65
ForwardRefExoticComponent,
76
PropsWithoutRef,
87
ComponentType,
8+
ComponentRef,
9+
ComponentProps,
910
} from "react";
1011
import { ErrorBoundary } from "./ErrorBoundary";
1112
import { ErrorBoundaryProps } from "./types";
1213

13-
export function withErrorBoundary<Props extends object>(
14-
component: ComponentType<Props>,
14+
export function withErrorBoundary<T extends ComponentType<any>>(
15+
component: T,
1516
errorBoundaryProps: ErrorBoundaryProps
16-
): ForwardRefExoticComponent<PropsWithoutRef<Props> & RefAttributes<any>> {
17-
const Wrapped = forwardRef<ComponentType<Props>, Props>(
18-
(props: Props, ref: ForwardedRef<ComponentType<Props>>) =>
19-
createElement(
20-
ErrorBoundary,
21-
errorBoundaryProps,
22-
createElement(component, { ...props, ref })
23-
)
17+
): ForwardRefExoticComponent<
18+
PropsWithoutRef<ComponentProps<T>> & RefAttributes<ComponentRef<T>>
19+
> {
20+
const Wrapped = forwardRef<ComponentRef<T>, ComponentProps<T>>((props, ref) =>
21+
createElement(
22+
ErrorBoundary,
23+
errorBoundaryProps,
24+
createElement(component, { ...props, ref })
25+
)
2426
);
2527

2628
// Format for display in DevTools

0 commit comments

Comments
 (0)