@jameshclrk/rxdb-hooks
Version:
React hooks for integrating with RxDB
38 lines (33 loc) • 810 B
text/typescript
import { useState, useEffect } from 'react';
import { RxCollection } from 'rxdb';
import useRxDB from './useRxDB';
function useRxCollection<T>(
dbName: string,
name: string
): RxCollection<T> | null {
const [collection, setCollection] = useState<RxCollection<T> | null>(null);
const db = useRxDB(dbName);
useEffect(() => {
if (!db) {
return;
}
const found = db[name];
if (found) {
setCollection(found);
}
if (db.newCollections$) {
const sub = db.newCollections$.subscribe(col => {
if (col[name]) {
// We don't unsubscribe so that we get notified
// and update collection if it gets deleted/recreated
setCollection(col[name]);
}
});
return () => {
sub.unsubscribe();
};
}
}, [db, name]);
return collection;
}
export default useRxCollection;