DataInterface hooks
useOpenedRow
The row a drawer shows: keep the id, read the row from the current page so a reload or an action updates it. The last snapshot only stands in while the row is absent from the page (filtered or paged away).
import { useOpenedRow } from '@ergasti/bricks';Examples
Keep an opened row fresh
Store the row you opened; the hook reads that row (matched by idOf) from the current page, so a reload or an action updates it. If the row is not on the page any more (filtered or paged away) the last snapshot stands in.
Code
import { Button, useOpenedRow } from '@ergasti/bricks';
import { useState } from 'react';
interface Row { id: number; name: string; status: string }
function Opened() {
const [rows, setRows] = useState<Row[]>([{ id: 1, name: 'Laila', status: 'Active' }, { id: 2, name: 'Karim', status: 'Active' }]);
const [opened, setOpened] = useOpenedRow(rows, (row) => row.id);
return (
<div style={{ display: 'grid', gap: 8 }}>
{rows.map((row) => <Button key={row.id} size="sm" onClick={() => setOpened(row)}>Open {row.name}</Button>)}
<Button size="sm" variant="ink" onClick={() => setRows(rows.map((r) => (r.id === 1 ? { ...r, status: 'Paused' } : r)))}>Pause Laila (reload)</Button>
<p>Opened: {opened ? `${opened.name}, ${opened.status}` : 'nothing'}</p>
</div>
);
}Signature
function useOpenedRow<T>(rows: readonly T[], idOf: (row: T) => string | number): readonly [(T & ({} | undefined)) | null, React.Dispatch<React.SetStateAction<T | null>>]Parameters
Prop
Type
Returns
readonly [(T & ({} | undefined)) | null, React.Dispatch<React.SetStateAction<T | null>>]
Related
Button · useDebouncedValue · useFocusTrap · useIsMobile · useModalLock
Source
src/hooks/useOpenedRow.ts (line 7) on GitHub.