Skip to content

Instantly share code, notes, and snippets.

@baetheus
Last active January 13, 2020 09:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baetheus/4f0153ddb2b433a7ac86dff3ba72d165 to your computer and use it in GitHub Desktop.
Save baetheus/4f0153ddb2b433a7ac86dff3ba72d165 to your computer and use it in GitHub Desktop.
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