Skip to Content
BBRICKSv0.7.4
DataHooks

useQueryState

useHashState for the page's search query (?ns.field=…), for apps that route on the path instead of the hash.

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

Examples

Filters in the query string

The same as useHashState, but in ?…: use it when the page's server also reads the params.

Code
import { useQueryState } from '@ergasti/bricks';

function Query() {
  const [state, setState] = useQueryState('docs-q', { view: 'grid' });
  return (
    <div style={{ display: 'flex', gap: 8 }}>
      {['grid', 'list'].map((view) => <button key={view} type="button" className="kit-btn" aria-pressed={state.view === view} onClick={() => setState({ view })}>{view}</button>)}
      <small>{JSON.stringify(state)}</small>
    </div>
  );
}

Signature

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

Parameters

Prop

Type

Returns

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

useAction · useDataLanguage · useDebouncedCallback · useDrawerResource

Source

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

On this page