Last active
January 13, 2020 09:08
-
-
Save baetheus/4f0153ddb2b433a7ac86dff3ba72d165 to your computer and use it in GitHub Desktop.
useRxjs POC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { some, none, fromNullable } from 'fp-ts/lib/Option'; | |
import { Observable } from 'rxjs' | |
import { useEffect, useState } from 'react' | |
const useRxjs = <T>(obs: Observable<T>, init?: T) => { | |
const [state, setState] = useState(fromNullable(init)); | |
const [errorState, setErrorState] = useState(none); | |
const [completeState, setCompleteState] = useState(false); | |
useEffect(() => obs | |
.subscribe( | |
n => setState(some(n)), | |
e => setErrorState(some(e)), | |
() => setCompleteState(true), | |
).unsubscribe, [obs, init]); | |
return [state, errorState, completeState]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment