Skip to main content

useRawField

Reads and updates the raw value of a field of any type, as stored by MoBro. All other field hooks are based on these two hooks.

Use them for field types without a dedicated hook, or when you need the value exactly as stored, e.g. the ValueMetricConfig of a metric field instead of the metric's value.

function useRawField<T>(props: {field: string}): T | undefined

function useSetRawField<T>(props: {field: string}): SetterOrUpdater<T | undefined>

Arguments

PropertyTypeDescription
fieldstringThe name of the field.

Returns

  • useRawField returns the stored value of the field, or undefined if the field has no value. The type T is not checked at runtime; see Types for the value types of the field types.
  • useSetRawField returns a setter function for the value of the field, see Setter Hooks.

Example

src/widgets/example/Example.tsx
import React from 'react'
import {useRawField, useSetRawField} from '@modbros/dashboard-sdk'
import type {ValueMetricConfig} from '@modbros/dashboard-core'

export default function Example() {
const metricConfig = useRawField<ValueMetricConfig>({field: 'metric'})
const setMetricConfig = useSetRawField<ValueMetricConfig>({field: 'metric'})

return (
<button onClick={() => setMetricConfig(undefined)}>
Clear metric {metricConfig?.id}
</button>
)
}