@@ -450,27 +450,30 @@ import * as React from 'react';
450450import { Subtract } from ' utility-types' ;
451451
452452// These props will be subtracted from original component type
453- interface WrappedComponentProps {
453+ interface InjectedProps {
454454 count: number ;
455455 onIncrement : () => any ;
456456}
457457
458- export const withState = <P extends WrappedComponentProps >(
459- WrappedComponent : React .ComponentType <P >
458+ export const withState = <WrappedProps extends InjectedProps >(
459+ WrappedComponent : React .ComponentType <WrappedProps >
460460) => {
461461 // These props will be added to original component type
462- interface Props {
462+ type HocProps = Subtract <WrappedProps , InjectedProps > & {
463+ // here you can extend hoc props
463464 initialCount? : number ;
464- }
465- interface State {
465+ };
466+ type HocState = {
466467 readonly count: number ;
467- }
468+ };
468469
469- return class WithState extends React .Component <Subtract < P , WrappedComponentProps > & Props , State > {
470+ return class WithState extends React .Component <HocProps , HocState > {
470471 // Enhance component name for debugging and React-Dev-Tools
471472 static displayName = ` withState(${WrappedComponent .name }) ` ;
473+ // reference to original wrapped component
474+ static readonly WrappedComponent = WrappedComponent ;
472475
473- readonly state: State = {
476+ readonly state: HocState = {
474477 count: Number (this .props .initialCount ) || 0 ,
475478 };
476479
@@ -479,14 +482,14 @@ export const withState = <P extends WrappedComponentProps>(
479482 }
480483
481484 render() {
482- const { ... remainingProps } = this .props ;
485+ const { ... restProps } = this .props as {} ;
483486 const { count } = this .state ;
484487
485488 return (
486489 <WrappedComponent
487- { ... remainingProps }
488- count = { count }
489- onIncrement = { this .handleIncrement }
490+ { ... restProps }
491+ count = { count } // injected
492+ onIncrement = { this .handleIncrement } // injected
490493 />
491494 );
492495 }
@@ -519,26 +522,29 @@ Adds error handling using componentDidCatch to any component
519522
520523` ` ` tsx
521524import * as React from ' react' ;
522- import { Subtract } from ' utility-types' ;
525+ // import { Subtract } from 'utility-types';
526+ type Omit < A , K> = Pick<A , Exclude<keyof A , K>>;
523527
524528const MISSING_ERROR = ' Error was swallowed during propagation.' ;
525529
526- interface WrappedComponentProps {
527- onReset? : () => any ;
530+ interface InjectedProps {
531+ onReset : () => any ;
528532}
529533
530- export const withErrorBoundary = <P extends WrappedComponentProps >(
531- WrappedComponent : React .ComponentType <P >
534+ export const withErrorBoundary = <WrappedProps extends InjectedProps >(
535+ WrappedComponent : React .ComponentType <WrappedProps >
532536) => {
533- interface Props { }
534- interface State {
537+ type HocProps = Omit <WrappedProps , keyof InjectedProps > & {
538+ // here you can extend hoc props
539+ };
540+ type HocState = {
535541 readonly error: Error | null | undefined ;
536- }
542+ };
537543
538- return class WithErrorBoundary extends React .Component <Subtract < P , WrappedComponentProps > & Props , State > {
544+ return class WithErrorBoundary extends React .Component <HocProps , HocState > {
539545 static displayName = ` withErrorBoundary(${WrappedComponent .name }) ` ;
540546
541- readonly state: State = {
547+ readonly state: HocState = {
542548 error: undefined ,
543549 };
544550
@@ -556,14 +562,14 @@ export const withErrorBoundary = <P extends WrappedComponentProps>(
556562 }
557563
558564 render() {
559- const { children, ... remainingProps } = this .props ;
565+ const { children, ... restProps } = this .props as { children : React . ReactNode } ;
560566 const { error } = this .state ;
561567
562568 if (error ) {
563569 return (
564570 <WrappedComponent
565- { ... remainingProps }
566- onReset = { this .handleReset }
571+ { ... restProps }
572+ onReset = { this .handleReset } // injected
567573 />
568574 );
569575 }
@@ -585,15 +591,15 @@ import { ErrorMessage } from '@src/components';
585591const ErrorMessageWithErrorBoundary =
586592 withErrorBoundary(ErrorMessage);
587593
588- const ErrorThrower = () => (
589- <button type = " button" onClick = { () => { throw new Error (` Catch this ! ` ); }} >
594+ const BrokenButton = () => (
595+ <button type=" button" onClick={() => { throw new Error(` Catch me ! ` ); }}>
590596 {` Throw nasty error ` }
591597 < / button >
592598);
593599
594600export default (() => (
595- <ErrorMessageWithErrorBoundary >
596- <ErrorThrower />
601+ <ErrorMessageWithErrorBoundary >
602+ <BrokenButton />
597603 </ErrorMessageWithErrorBoundary >
598604)) as React .SFC <{}>;
599605
0 commit comments