use-indexeddb-hook
v1.0.3
Published
A React hook to interact with IndexedDB.
Readme
use-indexeddb-hook
A React hook to interact with IndexedDB.
Demo
Feature Requests
Have a feature request? I'm listening! Please open an issue on GitHub using the following link, and I'll do my best to implement it within a day.
Installation
npm install use-indexeddb-hookUsage
import React from "react";
import useIndexedDB from "use-indexeddb-hook";
const App = () => {
const { db, error, addRecord, getAllRecords } = useIndexedDB("MyDB", "MyStore");
const handleAddRecord = async () => {
await addRecord({ name: "Test Record" });
const records = await getAllRecords();
console.log(records);
};
return (
<div>
<button onClick={handleAddRecord}>Add Record</button>
{error && <p>{error}</p>}
</div>
);
};
export default App;IndexedDB Operations
This section describes the available operations for interacting with the IndexedDB database.
db: The IndexedDB database instance.error: Any error that occurs during database operations.
Record Management
The following functions provide functionality for managing records within the IndexedDB store:
addRecord(record): Adds a new record to the store. Therecordparameter should be an object representing the data to be stored.updateRecord(record): Updates an existing record in the store. Therecordparameter should be an object containing the updated data.deleteRecord(id): Deletes a record from the store based on itsid.getAllRecords(): Retrieves all records currently stored in the IndexedDB store.getRecordById(id): Retrieves a single record from the store based on itsid.
License
This project is licensed under the MIT License. See the LICENSE file for details.
