Skip to content

Instantly share code, notes, and snippets.

@baetheus baetheus/useRxjs.ts
Last active Jul 10, 2019

Embed
What would you like to do?
useRxjs POC
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
You can’t perform that action at this time.