useSearchParamState

The useSearchParamState hook is similar to useState and usePersistedSetting, but stores the state value in the URL search params. This way the value is persisted if you reload the page, or share the url, but not on new sessions or on navigation.

Since the value is stored in the URL search params, it must be a string (or null to indicate no value). If the third parameter is set to true, multiple values can be stored.

The most common usage for this is probably filter selections.

Example

import { useData, useFilter, useSearchParamState } from "@olenbetong/appframe-react";

function MyComponent() {
  let [domains, setDomains] = useSearchParamState("domain", null, true);
  let myDomains = useData(dsMyDomains);
  useFilter(
    dsMyDataObject,
    domains.length ? `[Domain] IN (${domains.map((d) => `'${d}'`)})` : ""
  );

  function handleChange(evt) {
    let domains = Array.from(evt.target.querySelectorAll("option:checked")).map(
      (o) => o.value
    );

    setDomains(domains);
  }

  return (
    <select multiple>
      {myDomains.map((domain) => (
        <option key={domain.Domain} value={domain.Domain}>
          {domain.Domain} - {domain.Name}
        </option>
      ))}
    </select>
  );
}

API

Last updated

Was this helpful?