UNPKG

@jameshclrk/rxdb-hooks

Version:

React hooks for integrating with RxDB

35 lines (34 loc) 1.19 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var react_1 = require("react"); var useRxDB_1 = __importDefault(require("./useRxDB")); function useRxCollection(dbName, name) { var _a = (0, react_1.useState)(null), collection = _a[0], setCollection = _a[1]; var db = (0, useRxDB_1.default)(dbName); (0, react_1.useEffect)(function () { if (!db) { return; } var found = db[name]; if (found) { setCollection(found); } if (db.newCollections$) { var sub_1 = db.newCollections$.subscribe(function (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 function () { sub_1.unsubscribe(); }; } }, [db, name]); return collection; } exports.default = useRxCollection;