Skip to Content
BBRICKSv0.7.4
DataHooks

useHashState

A screen's state (filters, page, selected id) in the hash query under namespace (ns.field; bare for ''): a patch merges in with one history write, a value equal to its default leaves the URL, params the screen does not own are kept. Re-read on Back / Forward, hash links and every other URL-state write (bc:url). Values are strings; parse numbers at the call site. Returns [state, patch, reset].

import { useHashState } from '@ergasti/bricks';

Examples

Filters in the hash

useHashState(namespace, defaults) keeps an object in the URL hash query (#view?codes.q=kari); a value equal to its default leaves the URL; Back and Forward re-read it. The setter writes one history entry for the whole patch, and the third item resets.

Code
import { Button, FilterChips, useHashState } from '@ergasti/bricks';

function Filters() {
  const [filters, setFilters, reset] = useHashState('docs-codes', { q: '', status: 'all' });
  return (
    <div style={{ display: 'grid', gap: 10 }}>
      <input aria-label="Search codes" placeholder="Search codes" value={filters.q} onChange={(e) => setFilters({ q: e.target.value })} />
      <FilterChips label="Status" value={filters.status} onChange={(status) => setFilters({ status })} options={[{ value: 'all', label: 'All' }, { value: 'active', label: 'Active' }, { value: 'paused', label: 'Paused' }]} />
      <div><Button size="sm" onClick={reset}>Reset</Button> <small>{JSON.stringify(filters)}</small></div>
    </div>
  );
}

Signature

function useHashState<S>(namespace: string, defaults: S, options?: WriteOptions | undefined): [S, UrlStateSetter<S>, () => void]

Parameters

Prop

Type

Returns

[S, UrlStateSetter<S>, () => void]

Filters · FilterChips · Button · useAction · useDataLanguage · useDebouncedCallback · useDrawerResource

Source

src/data/useUrlState.ts (line 49) on GitHub.

On this page