@feltdb/react
v0.4.4
Published
FeltDB React Bindings - React hooks and components for FeltDB
Maintainers
Readme
@feltdb/react
React hooks for FeltDB. Provides easy integration with React applications.
Installation
npm install @feltdb/react @feltdb/coreQuick Start
import React from 'react';
import { useCollection } from '@feltdb/react';
import { db } from './feltdb';
function TodoApp() {
const { data: todos, insert, update, remove } = useCollection(db.collection('todos'));
const [input, setInput] = React.useState('');
const handleAdd = async () => {
if (input.trim()) {
await insert({ title: input, completed: false });
setInput('');
}
};
return (
<div>
<h1>Todos</h1>
<input
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyPress={(e) => e.key === 'Enter' && handleAdd()}
/>
<button onClick={handleAdd}>Add</button>
<ul>
{todos.map((todo) => (
<li key={todo.id}>
<input
type="checkbox"
checked={todo.completed}
onChange={(e) => update(todo.id, { completed: e.target.checked })}
/>
<span>{todo.title}</span>
<button onClick={() => remove(todo.id)}>Delete</button>
</li>
))}
</ul>
</div>
);
}Hooks
useCollection(collection, options?)
Hook for working with a collection.
Parameters:
collection- Collection instance from FeltDBoptions(optional) - Query and subscription options
Returns: Object with:
data- Array of itemsloading- Whether data is loadingerror- Any error that occurredinsert(item)- Insert a new itemupdate(id, updates)- Update an itemremove(id)- Remove an item
useCapability(capability?)
Hook for executing capabilities.
Parameters:
capability- Capability instance
Returns: Object with:
execute(...args)- Execute the capabilityloading- Whether executingerror- Any error that occurredresult- Last result
License
MIT
