useMetricFieldValueType
Determines the kind of a metric value by inspecting the value itself.
function useMetricFieldValueType(
value: ChannelValue | MetricValue | MetricValueType
): 'unknown' | 'date' | 'string' | 'number'
tip
To branch on the value type defined by the metric's type, use channelValue.type.valueType instead, which
distinguishes all value types.
Arguments
| Argument | Type | Description |
|---|---|---|
| value | ChannelValue, MetricValue or MetricValueType | The value to inspect. For a ChannelValue or MetricValue, the contained value is used. |
Returns
| Result | If the value is |
|---|---|
number | A number |
date | A string containing an ISO date |
string | Any other string |
unknown | null, or a falsy primitive value |
Example
src/widgets/example/Example.tsx
import React from 'react'
import {useMetricField, useMetricFieldValueType} from '@modbros/dashboard-sdk'
export default function Example() {
const channelValue = useMetricField({field: 'metric'})
const valueType = useMetricFieldValueType(channelValue)
if (valueType === 'number') {
return <strong>{Number(channelValue?.value.value).toFixed(1)}</strong>
}
return <span>{channelValue?.value.value}</span>
}