Skip to content

useParam

Reads and writes a single value on the ROS parameter server, like max_vel_x. Fetches the current value on mount by default.

tsx
import { useParam } from "roshooks";

const { value, loading, error, refresh, setValue, deleteValue } = useParam<number>("/max_vel_x");

Parameters

ParameterTypeDescription
namestringThe parameter name, e.g. "/max_vel_x".
options.autoFetchboolean (default true)Fetch the current value automatically on mount and whenever name changes.

Returns

FieldTypeDescription
valueT | nullThe current value, or null before the first successful fetch.
loadingbooleantrue while a get/set/delete call is in flight.
errorstring | nullThe failure message from the most recent failed call.
refresh() => Promise<T>Re-fetches the value from the parameter server.
setValue(value: T) => Promise<void>Sets the value on the parameter server.
deleteValue() => Promise<void>Deletes the parameter on the parameter server.

Example

tsx
function MaxVelocityControl() {
  const { value, loading, setValue } = useParam<number>("/max_vel_x");

  return (
    <input
      type="number"
      value={value ?? 0}
      disabled={loading}
      onChange={(e) => setValue(Number(e.target.value)).catch(() => {})}
    />
  );
}

Disabling the automatic fetch

tsx
const { value, refresh } = useParam<number>("/max_vel_x", { autoFetch: false });

// fetch on demand instead, e.g. from a button
<button onClick={() => refresh()}>Load</button>;

Released under the BSD-3 License.