@@ -10,12 +10,14 @@ import {
1010 * Configuration for the withLifecycle chainable component
1111 */
1212export type WithLifecycleOptions < C > = {
13- componentDidMount : ( ) => C ;
14- componentWillUnmount ?: ( c : C ) => void ;
13+ init : ( ) => C ;
14+ componentDidMount ?: ( ) => C ;
15+ componentWillUnmount ?: ( c : C ) => C ;
16+ componentWillUpdate ?: ( c : C ) => C ;
1517 shouldComponentUpdate ?: ( c : C ) => boolean ;
1618} ;
1719
18- type WithLifecycleProps < C > = RenderPropsProps < WithLifecycleOptions < C > , { } > ;
20+ type WithLifecycleProps < C > = RenderPropsProps < WithLifecycleOptions < C > , C > ;
1921
2022/**
2123 * A Render Prop component that mimics the react Component API
@@ -24,25 +26,36 @@ export class WithLifecycle<C> extends React.Component<
2426 WithLifecycleProps < C > ,
2527 { }
2628> {
29+ value : C
2730 constructor ( props : WithLifecycleProps < C > ) {
2831 super ( props ) ;
32+ this . value = this . props . init ( ) ;
2933 }
3034
3135 componentDidMount ( ) {
32- this . context = this . props . componentDidMount ( ) ;
36+ if ( this . props . componentDidMount ) {
37+ this . value = this . props . componentDidMount ( ) ;
38+ }
3339 }
3440 componentWillUnmount ( ) {
3541 this . props . componentWillUnmount &&
36- this . props . componentWillUnmount ( this . context ) ;
42+ this . props . componentWillUnmount ( this . value ) ;
3743 }
3844 shouldComponentUpdate ( ) {
3945 return this . props . shouldComponentUpdate
40- ? this . props . shouldComponentUpdate ( this . context )
46+ ? this . props . shouldComponentUpdate ( this . value )
4147 : true ;
4248 }
4349
50+ UNSAFE_componentWillUpdate ( nextProps : WithLifecycleProps < C > ) {
51+ if ( nextProps . componentWillUpdate ) {
52+ this . value = nextProps . componentWillUpdate ( this . value ) ;
53+ }
54+ }
55+
4456 render ( ) {
45- return this . props . children ( { } ) ;
57+ console . log ( 'Rendering?' , this . value )
58+ return this . props . children ( this . value ) ;
4659 }
4760}
4861
@@ -52,6 +65,6 @@ export class WithLifecycle<C> extends React.Component<
5265 */
5366export function withLifecycle < C > (
5467 options : WithLifecycleOptions < C >
55- ) : ChainableComponent < { } > {
68+ ) : ChainableComponent < C > {
5669 return fromRenderProp < WithLifecycleProps < C > > ( WithLifecycle , options ) ;
5770}
0 commit comments