Skip to content

Instantly share code, notes, and snippets.

@arv
Created October 20, 2021 20:48
Show Gist options
  • Save arv/c8301854c8b7cc03ee8ac76f67892944 to your computer and use it in GitHub Desktop.
Save arv/c8301854c8b7cc03ee8ac76f67892944 to your computer and use it in GitHub Desktop.
import { useEffect, useState } from "react";
import { MutatorDefs, Replicache, ReplicacheOptions } from "replicache";
export function useReplicache<MD extends MutatorDefs>(
opts: ReplicacheOptions<MD>
): Replicache<MD> | null {
const [rep, setRep] = useState<Replicache<MD> | null>(null);
useEffect(() => {
const r = new Replicache(opts);
setRep(r);
return () => {
r.close();
};
}, [opts]);
return rep;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment