diff --git a/lib/interfaces/plug.ts b/lib/interfaces/plug.ts index e1b3b23..31793f7 100644 --- a/lib/interfaces/plug.ts +++ b/lib/interfaces/plug.ts @@ -1,13 +1,3 @@ import { Observable } from "rxjs"; -export type TStream = (stream: any) => Observable; - -export interface ILifecycle { - componentDidMount: () => void; - componentWillUnmount: () => void; -} - -export interface IPlugState { - hasEmmited: boolean; - innerData?: any; -} \ No newline at end of file +export type TStream = (stream: any) => Observable; \ No newline at end of file diff --git a/lib/plug.tsx b/lib/plug.tsx index 25c6a0e..5f3506b 100644 --- a/lib/plug.tsx +++ b/lib/plug.tsx @@ -1,51 +1,26 @@ import * as React from 'react'; -import { Component } from 'react'; -import { Subscription } from 'rxjs'; -import { TStream, ILifecycle, IPlugState } from './interfaces/plug'; - - -export const plug = (stream: TStream, lifecycleHooks: Partial = {}, -) => (WrappedComponent: any) => - class PluggedComponent extends Component { - - _streamSubscription!: Subscription; - - constructor(props: any) { - super(props); - - this.state = { - hasEmmited: false, - }; - } - - public componentDidMount() { - this._streamSubscription = stream(this.props).subscribe((data: any) => - this.setState({ - hasEmmited: true, - innerData: data, - }), - ); - - if (lifecycleHooks.componentDidMount) { - lifecycleHooks.componentDidMount(); - } - } - - public componentWillUnmount() { - this._streamSubscription.unsubscribe(); - - if (lifecycleHooks.componentWillUnmount) { - lifecycleHooks.componentWillUnmount(); - } +import { useState, useEffect } from 'react'; +import { TStream } from './interfaces/plug'; + +export const plug = (stream: TStream) => (WrappedComponent: any) => (props: T) => { + const [hasEmmited, setHasEmitted] = useState(false); + const [innerData, setInnerData] = useState({}); + + useEffect(() => { + const streamSubscription = stream(props) + .subscribe((data: any) => { + setHasEmitted(true); + setInnerData(data); + }); + + return () => { + streamSubscription.unsubscribe(); } + }, []) - public render() { - const { hasEmmited, innerData } = this.state; - - if (hasEmmited) { - return ; - } + if (hasEmmited) { + return ; + } - return null; - } - } \ No newline at end of file + return null; +}