Registry / database / react-sqlite-hook

react-sqlite-hook

JSON →
library3.2.1jsnpmunverified

A React Hook wrapper around @Capacitor-community/sqlite, providing a simple interface for using SQLite databases in Capacitor-based mobile apps. Version 3.2.1 is the latest stable release, with active development and occasional minor updates. It simplifies database initialization, queries, and transactions inside React components using hooks like useSQLite. Key differentiators: fully typed with TypeScript, follows React best practices, and integrates seamlessly with Capacitor's lifecycle. Requires React 18+ and the Capacitor SQLite plugin.

npm install react-sqlite-hook
INSTALL
IMPORT
SIG · REACT-SQLITE-HOOK
R
react-sqlite-hook
databasejavascriptv3.2.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

useSQLite
import { useSQLite } from 'react-sqlite-hook'
import useSQLite from 'react-sqlite-hook'
Named export, not default. ESM only.
SQLiteProvider
import { SQLiteProvider } from 'react-sqlite-hook'
const { SQLiteProvider } = require('react-sqlite-hook')
CJS require not supported; library is ESM-only.
openDatabase
import { openDatabase } from 'react-sqlite-hook'
Utility function, not a hook. Used outside components for initialization.

Shows how to wrap your app in SQLiteProvider and use useSQLite hook to create a table, insert a row, and query.

import React from 'react'; import { SQLiteProvider, useSQLite } from 'react-sqlite-hook'; function App() { return ( <SQLiteProvider> <DatabaseDemo /> </SQLiteProvider> ); } function DatabaseDemo() { const { execute, query } = useSQLite(); const [data, setData] = React.useState([]); React.useEffect(() => { const init = async () => { await execute('CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, value TEXT)'); await execute('INSERT INTO test VALUES (?,?)', [1, 'hello']); const result = await query('SELECT * FROM test'); setData(result.values); }; init(); }, []); return <div>{JSON.stringify(data)}</div>; } export default App;
Debug
Known issues
breakingReact 18 or later is required. Version 2.x supported React 16/17, but 3.x dropped support for older versions.
fix
Update React to 18+ or stay on react-sqlite-hook 2.x if you cannot upgrade React.
affects: >=3.0.0
breakingRemoved default export. In v2 you could `import SQLite from 'react-sqlite-hook'`, but v3 only exports named exports.
fix
Replace default import with named imports like `import { useSQLite } from 'react-sqlite-hook'`.
affects: >=3.0.0
deprecatedThe `useSQLite` hook's `open` method is deprecated; use `openDatabase` instead.
fix
Replace `const db = await open()` with `const db = await openDatabase('mydb')`.
affects: >=3.2.0
gotchaThe plugin requires Capacitor's native SQLite plugin to be installed and configured. The hook will fail silently if the native plugin is missing.
fix
Ensure `@capacitor-community/sqlite` is added to your Capacitor project and synced to native builds.
affects: >=0.0.0
gotchaIn React Strict Mode, effects run twice which may cause duplicate database operations. Wrap calls in try-catch or use refs to guard.
fix
Use a ref to track initialization: `const initialized = useRef(false);`.
affects: >=3.0.0
Errors
Common errors & fixes
Error: Cannot find module 'react-sqlite-hook'
Package not installed or import path is wrong.
fix
Run `npm install react-sqlite-hook` and ensure you are importing from the correct package name.
TypeError: Cannot destructure property 'execute' of (0 , _reactSqliteHook.useSQLite)(...) as it is undefined.
useSQLite hook is used outside of SQLiteProvider context.
fix
Wrap the component tree in <SQLiteProvider>.
react-sqlite-hook: The native SQLite plugin is not available.
@capacitor-community/sqlite native plugin not installed or not synced.
fix
Run `npm install @capacitor-community/sqlite` then `npx cap sync`.
Warning: React 18 features like useId may not work correctly in React 17.
Using react-sqlite-hook v3 with React 17.
fix
Upgrade to React 18 or downgrade react-sqlite-hook to v2.
Upgrade
Version history
3.2.1latest on npm
Audit
Dependencies
@capacitor-community/sqliterequiredCore database plugin that this hook wraps
reactrequiredPeer dependency; requires React 18+
Agent activity
8 hits · last 30 days
node
6
Resources
react-sqlite-hook — npm install react-sqlite-hook · libregistry