Skip to main content

Field Hooks

Field hooks read the values users configured for the fields of a widget. Every hook takes the name of the field as field argument, and re-renders the widget when the value changes.

Within a Repeater, field hooks read the fields of the current repeater item.

Field typeHook
stringuseStringField
numberuseNumberField
checkboxuseCheckboxField
select, radiouseSelectField
fileuseFileField, useFilesField
fontuseFontField
metricuseMetricField, useMemoizedMetricField, useIsMetricFieldConfigured
coloruseColorField
repeateruseRepeaterField
actionuseActionField
anyuseRawField, useFieldConfig

Setter Hooks

Setter hooks update the value of a field from within the widget, just as if the user changed it in the dashboard builder. The following setter hooks exist:

HookValue type
useSetStringFieldstring
useSetNumberFieldnumber
useSetCheckboxFieldboolean
useSetSelectFieldstring
useSetColorFieldValueColor
useSetRawFieldany

There are no dedicated setter hooks for the other field types; use useSetRawField for them.

Setter hooks take the field as argument and return a setter function, a SetterOrUpdater of Recoil. Like the setter of useState, it takes either the new value or a function receiving the previous value:

src/widgets/example/Example.tsx
import React from 'react'
import {useSetStringField, useStringField} from '@modbros/dashboard-sdk'

export default function Example() {
const text = useStringField({field: 'example'})
const setText = useSetStringField({field: 'example'})

return <button onClick={() => setText((previous) => `${previous ?? ''}!`)}>{text}</button>
}