This is a React hook to mutate the record store.
const mutations = useRecordMutations();
useRecordMutations()
Call useRecordMutations
at the top level of your component to access the record store mutation functions.
This hook returns a RecordMutations
object.
<Store>
.RecordMutations
updateRecords(records: Record[])
Update records in the store.
The local state is updated immediately.
Records are updated on the server as soon as possible.
deleteRecords(keys: RecordKey[])
Delete records in the store.
The local state is updated immediately.
Records are deleted on the server as soon as possible.
To update a record, we create a new version with the updated content and call the updateRecords
function.
import {
useRecordHelpers,
useRecordMutations,
} from "./baq/store.js";
import {ItemRecord} from "./baq/itemRecord.js";
function RenameItem({itemRecord}) {
const {entity} = useRecordHelpers();
const {updateRecords} = useRecordMutations();
const [name, setName] = useState("");
const onSaveItemClick = () => {
const updatedContent = {name};
const updatedRecord = ItemRecord.update(
itemRecord,
entity,
updatedContent
);
updateRecords([updatedRecord]);
};
return (
<>
<input type="text" value={name} onChange={setName} />
<button onClick={onSaveItemClick}></button>
</>
);
}
To delete a record, we call the deleteRecords
function with its key.
import {Record} from "@baqhub/sdk";
import {useRecordMutations} from "./baq/store.js";
function Item({itemRecord}) {
const {deleteRecords} = useRecordMutations();
const onDeleteClick = () => {
const itemKey = Record.toKey(itemRecord);
deleteRecords([itemKey]);
};
return (
<>
<span>{itemRecord.content.name}</span>
<button onClick={onDeleteClick}>Delete</button>
</>
);
}