Skip to main content

useSensorType hooks

These hooks manage sensor type data used throughout the platform. They provide read access to sensor type lists and individual records, plus mutations for create, update, and delete operations. Built on SWR for caching and revalidation, with adapter-based data fetching for flexibility across different API implementations.

Hooks

HookDescription
useSensorTypesFetches all sensor types
useSensorTypeFetches a single sensor type by ID, with update and delete mutations
useCreateSensorTypeCreates a new sensor type

useSensorTypes

Fetches the complete list of sensor types. Returns an array that defaults to empty while loading.

Signature

function useSensorTypes(): UseSensorTypesReturn

Parameters

None.

Returns

FieldTypeDescription
sensorTypesSensorType[]Array of sensor types, defaults to []
isLoadingbooleanSWR loading state
isErrorError | undefinedSWR error state

Example

const { sensorTypes, isLoading } = useSensorTypes();

if (isLoading) return <Skeleton />;

return (
<ul>
{sensorTypes.map((type) => (
<li key={type.id}>{type.name}</li>
))}
</ul>
);

useSensorType

Fetches a single sensor type by ID. Also provides updateSensorType and deleteSensorType mutation triggers.

Signature

function useSensorType(id: number | null): UseSensorTypeReturn

Parameters

ParamTypeRequiredDescription
idnumber | nullYesSensor type ID to fetch. Pass null to skip fetching.

Returns

FieldTypeDescription
sensorTypeSensorType | nullThe fetched sensor type, or null if not loaded
isLoadingbooleanSWR loading state
isErrorError | undefinedSWR error state
updateSensorType(arg: Partial<SensorType>) => Promise<SensorType>Trigger to update the sensor type
isMutatingbooleanWhether an update is in progress
updateErrorError | undefinedError from the most recent update attempt
updatedDataSensorType | undefinedData returned from the most recent update
deleteSensorType() => Promise<SensorType>Trigger to delete the sensor type
isDeletingbooleanWhether a delete is in progress
deleteErrorError | undefinedError from the most recent delete attempt

Example

const { sensorType, isLoading, updateSensorType, deleteSensorType } = useSensorType(42);

const handleRename = async () => {
await updateSensorType({ name: "Updated Name" });
};

const handleDelete = async () => {
await deleteSensorType();
};

Notes

On successful update, revalidates both the individual sensor type cache key (["sensorType", id]) and the list (["sensorTypes"]).

On successful delete, removes the individual entry from cache without revalidation and revalidates the list.


useCreateSensorType

Creates a new sensor type record.

Signature

function useCreateSensorType(): UseCreateSensorTypeReturn

Parameters

None.

Returns

FieldTypeDescription
createSensorType(arg: { sensorTypeData: Partial<SensorType> }) => Promise<SensorType>Trigger to create a sensor type
isMutatingbooleanWhether creation is in progress
createErrorError | undefinedError from the most recent create attempt
createdDataSensorType | undefinedData returned from the most recent creation

Example

const { createSensorType, isMutating } = useCreateSensorType();

const handleCreate = async () => {
await createSensorType({ sensorTypeData: { name: "Temperature" } });
};

return (
<Button onClick={handleCreate} disabled={isMutating}>
Add Sensor Type
</Button>
);

Notes

On successful creation, revalidates the ["sensorTypes"] list cache key.