Skip to content
Forms, switch product

Runtime Methods

setData and setMeta let you push state changes into a mounted form without re-initialising it. Use them to pre-fill fields after an async fetch, sync the form with external state, or toggle meta-driven behaviour at runtime. setHealth reports an error the form cannot see by itself, such as a server that cannot be reached.

setData and setMeta replace their respective state objects entirely: there is no deep merge.

In Lit and Angular, the form element exists before the form has started. A setData, setMeta or setHealth call in that time is kept. The last setData or setMeta call replaces the data or meta of the config, like a call after the start. A setHealth call is applied after the start: the first formHealth value is the health of the started form (ok, or an error of the start such as an error in the definition), and the health of the call comes next.

A new config starts a new form. Which form gets a call made at that time depends on the framework:

  • Lit: a call after you assign the new config and before the form starts with it goes to the new form. The old form gets nothing.
  • React: the form starts with the new config in an effect, and an object ref (useRef) gets the handle of the new form there. So a call from a useEffect of the component that renders the form reaches the new form. A call from a useLayoutEffect, or through a handle that a callback ref stored, runs before the start and is lost.
  • Angular and Vue: a call before the form gets the new config goes to the old form, and then the new form replaces it.

Each framework has its own mechanism for obtaining a handle to the form component instance:

import { useRef } from 'react';
import { GuiForm } from '@golemui/gui-react';
import type { FormComponentHandle } from '@golemui/react';
export function MyForm({ config }) {
const formRef = useRef<FormComponentHandle>(null);
return <GuiForm ref={formRef} config={config} />;
}

Replaces the form’s current data state. All field values are overwritten with the supplied object.

import { useRef } from 'react';
import { GuiForm } from '@golemui/gui-react';
import type { FormComponentHandle } from '@golemui/react';
export function MyForm({ config }) {
const formRef = useRef<FormComponentHandle>(null);
async function loadUser(userId: string) {
const user = await fetchUser(userId);
formRef.current?.setData({ name: user.name, email: user.email });
}
return (
<>
<button onClick={() => loadUser('42')}>Load user</button>
<GuiForm ref={formRef} config={config} />
</>
);
}

Replaces the form’s current meta state. Meta values are available inside the form definition for template interpolation ({{$meta.key}}) and conditions ($meta.key === true).

import { useRef } from 'react';
import { gui } from '@golemui/gui-shared';
import { GuiForm } from '@golemui/gui-react';
import type { FormComponentHandle } from '@golemui/react';
const formDef = [
gui.displays.alert('status-banner', {
text: 'Connection: {{$meta.status}}',
}),
];
const config = { formDef, meta: { status: 'offline' } };
export function MyForm() {
const formRef = useRef<FormComponentHandle>(null);
function goOnline() {
formRef.current?.setMeta({ status: 'online' });
}
return (
<>
<button onClick={goOnline}>Connect</button>
<GuiForm ref={formRef} config={config} />
</>
);
}
MethodSignatureDescription
setData(data: Record<string, any>) => voidReplaces the form’s data state. All field values are overwritten.
setMeta(meta: Record<string, any>) => voidReplaces the form’s meta state. Used in template interpolations and conditions.

Sets the form health from outside, next to setData and setMeta. Use it for an error the form cannot see by itself, for example a form that saves each change to a server that goes down.

  • setHealth({ status: 'errored', code, message }) shows the error like one of the form’s own: the form sends formHealth, shows the formHealthBoundary and stops updating. A setData call does not change the inputs while it is frozen.
  • setHealth({ status: 'ok' }) hides the boundary, sends formHealth again, and the user can type again. Data written with setData while the form was frozen shows with the next change.
import { useRef } from 'react';
import { GuiForm } from '@golemui/gui-react';
import type { FormComponentHandle } from '@golemui/react';
export function MyForm({ config }) {
const formRef = useRef<FormComponentHandle>(null);
function serverDown() {
formRef.current?.setHealth({
status: 'errored',
code: 10_001,
message: 'The server cannot be reached',
});
}
function serverBack() {
formRef.current?.setHealth({ status: 'ok' });
}
return <GuiForm ref={formRef} config={config} />;
}