Skip to content

Commit 2d234ef

Browse files
committed
Fixed issue with HOC Fixed #111
1 parent ba6e67c commit 2d234ef

File tree

2 files changed

+11
-32
lines changed

2 files changed

+11
-32
lines changed

playground/src/hoc/with-error-boundary.tsx

+5-23
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
1-
import * as React from 'react';
2-
import { Subtract } from 'utility-types';
1+
import React from 'react';
32

43
const MISSING_ERROR = 'Error was swallowed during propagation.';
54

6-
// These props will be subtracted from base component props
7-
interface InjectedProps {
8-
onReset: () => void;
9-
}
10-
11-
export const withErrorBoundary = <BaseProps extends InjectedProps>(
12-
_BaseComponent: React.ComponentType<BaseProps>
5+
export const withErrorBoundary = <BaseProps extends {}>(
6+
BaseComponent: React.ComponentType<BaseProps>
137
) => {
14-
// fix for TypeScript issues: https://github.com/piotrwitek/react-redux-typescript-guide/issues/111
15-
const BaseComponent = _BaseComponent as React.ComponentType<InjectedProps>;
16-
17-
type HocProps = Subtract<BaseProps, InjectedProps> & {
8+
type HocProps = {
189
// here you can extend hoc with new props
1910
};
2011
type HocState = {
@@ -40,21 +31,12 @@ export const withErrorBoundary = <BaseProps extends InjectedProps>(
4031
// TODO: send error report to service provider
4132
};
4233

43-
handleReset = () => {
44-
this.setState({ error: undefined });
45-
};
46-
4734
render() {
4835
const { children, ...restProps } = this.props;
4936
const { error } = this.state;
5037

5138
if (error) {
52-
return (
53-
<BaseComponent
54-
onReset={this.handleReset} // injected
55-
{...restProps}
56-
/>
57-
);
39+
return <BaseComponent {...(restProps as BaseProps)} />;
5840
}
5941

6042
return children;

playground/src/hoc/with-state.tsx

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
import * as React from 'react';
2-
import { Subtract } from 'utility-types';
1+
import React from 'react';
2+
import { Diff } from 'utility-types';
33

4-
// These props will be subtracted from base component props
4+
// These props will be injected into the base component
55
interface InjectedProps {
66
count: number;
77
onIncrement: () => void;
88
}
99

1010
export const withState = <BaseProps extends InjectedProps>(
11-
_BaseComponent: React.ComponentType<BaseProps>
11+
BaseComponent: React.ComponentType<BaseProps>
1212
) => {
13-
// fix for TypeScript issues: https://github.com/piotrwitek/react-redux-typescript-guide/issues/111
14-
const BaseComponent = _BaseComponent as React.ComponentType<InjectedProps>;
15-
16-
type HocProps = Subtract<BaseProps, InjectedProps> & {
13+
type HocProps = Diff<BaseProps, InjectedProps> & {
1714
// here you can extend hoc with new props
1815
initialCount?: number;
1916
};
@@ -43,7 +40,7 @@ export const withState = <BaseProps extends InjectedProps>(
4340
<BaseComponent
4441
count={count} // injected
4542
onIncrement={this.handleIncrement} // injected
46-
{...restProps}
43+
{...(restProps as BaseProps)}
4744
/>
4845
);
4946
}

0 commit comments

Comments
 (0)