Skip to Content
Octopus Energy Packages
API ReferenceStoragenuqs

nuqs Client

APIs exported from @octopus-energy/measurements/storage/nuqs/client

Use this entry point in client components that persist measurement state in URL search params.

import {
  createNuqsAdapter,
  createPreferencesSearchParams,
  createTimeframeSearchParams,
} from "@octopus-energy/measurements/storage/nuqs/client";

createTimeframeSearchParams

Creates a client-safe nuqs parser config for useTimeframe state.

import { createTimeframeSearchParams } from "@octopus-energy/measurements/storage/nuqs/client";
createTimeframeSearchParams

Prop

Type

period and interval default to "week". date has no parser default. Pass stable non-date URL defaults through the parser factory when the omitted URL value should mean something else. Resolve dynamic date defaults separately and pass them to useTimeframe({ defaults }).

import { createTimeframeSearchParams } from "@octopus-energy/measurements/storage/nuqs/client";
import type { TimeframeSearchParamDefaults } from "@octopus-energy/measurements/storage/nuqs";
const timeframeDefaults = {
  period: "month",
} as const satisfies TimeframeSearchParamDefaults;

const unscoped = createTimeframeSearchParams({ defaults: timeframeDefaults });
// URL: ?date=2024-01-23&period=week

const scoped = createTimeframeSearchParams({
  defaults: timeframeDefaults,
  scopes: ["electricity", "gas"],
});
// URL: ?electricity.date=2024-01-23&gas.period=week

Date defaults are dynamic state

createTimeframeSearchParams ignores defaults.date and warns during development. Resolve anchor dates outside the parser config, then pass the resolved date to useTimeframe({ defaults: { date } }).

Shared default and option types are documented in nuqs Shared Types.


createPreferencesSearchParams

Creates a client-safe nuqs parser config for usePreferences state.

import { createPreferencesSearchParams } from "@octopus-energy/measurements/storage/nuqs/client";
createPreferencesSearchParams

Prop

Type

Preference parser defaults are stable URL defaults: standingCharge = true, tax = true, unit = "kWh", and view = "chart". Override them in the parser factory when the omitted URL value should mean a different stable app default.

import { createPreferencesSearchParams } from "@octopus-energy/measurements/storage/nuqs/client";
import type { PreferencesSearchParamDefaults } from "@octopus-energy/measurements/storage/nuqs";
const preferenceDefaults = {
  unit: "cost",
} as const satisfies PreferencesSearchParamDefaults;

const unscoped = createPreferencesSearchParams({
  defaults: preferenceDefaults,
});
// URL: ?view=table&unit=kWh

const scoped = createPreferencesSearchParams({
  defaults: preferenceDefaults,
  scopes: ["electricity", "gas"],
});
// URL: ?electricity.view=table&gas.unit=kWh

createNuqsAdapter

Creates a StorageAdapter from a nuqs useQueryStates result. The hook state is controlled by the state object passed to the adapter; when nuqs rerenders your component with changed URL state, the hook derives its next state from that new snapshot. The adapter's subscribe method is intentionally a no-op because useQueryStates already drives the React rerender.

import { createNuqsAdapter } from "@octopus-energy/measurements/storage/nuqs/client";
createNuqsAdapter

Prop

Type

const [searchParams, setSearchParams] = useQueryStates(createPreferencesSearchParams());
createNuqsAdapter(searchParams, setSearchParams);

const [scopedParams, setScopedParams] = useQueryStates(
  createPreferencesSearchParams({
    scopes: ["electricity", "gas"],
  }),
);
createNuqsAdapter(scopedParams, setScopedParams, { scope: "electricity" });

For SSR/RSC routes, parse the same search params on the server, resolve the initial timeframe with resolveTimeframe, pass it to the client component, and use it as the useTimeframe default:

const [timeframeSearchParams, setTimeframeSearchParams] = useQueryStates(
  createTimeframeSearchParams(),
);

const [timeframe] = useTimeframe({
  defaults: initialTimeframe,
  storage: createNuqsAdapter(timeframeSearchParams, setTimeframeSearchParams),
  timezone: initialTimeframe.timezone,
});

This aligns the first client render with the timeframe resolved on the server. Keep client and server parser configs aligned when using custom parser defaults.


On this page