Run a Cratis Arc command from React
Use a generated Cratis Arc command proxy to submit a React form without writing a separate API client. CommandDialog binds the fields to the command, displays validation feedback, and prevents another submission while the command is running. This recipe registers an author from a dialog.
Call the proxy, don’t write a client
Section titled “Call the proxy, don’t write a client”The generated proxy carries the command’s shape and supported validation rules into TypeScript. Use that contract rather than redeclaring it beside a fetch call. The server still validates every submitted command.
Before you start
Section titled “Before you start”Follow your backend’s setup and proxy-generation guide and the full-stack tutorial setup through the React bootstrap. You need the RegisterAuthor command, its generated proxy, the Components dependencies, and the providers and dialog composition shown there. For the C# backend, generate the proxy with the configured Debug proxy-generation build. This standalone flow does not require Cratis Chronicle.
For an inline form instead of a dialog, frontend getting started shows Arc’s CommandForm with Components fields and a route without Components.
-
Pass the generated command constructor to
CommandDialogand bind an input to itsnameproperty. The dialog creates the command instance and supplies confirm/cancel buttons:AddAuthor.tsx import { useState } from 'react';import { Guid } from '@cratis/fundamentals';import { CommandDialog } from '@cratis/components/CommandDialog';import { InputTextField } from '@cratis/components/CommandForm';import { RegisterAuthor } from './Authors/RegisterAuthor'; // generated proxyexport const AddAuthor = () => {const [id] = useState(() => Guid.create());return (<CommandDialog<RegisterAuthor> command={RegisterAuthor} title="Add author" okLabel="Add" initialValues={{ id }}><InputTextField<RegisterAuthor> value={i => i.name} title="Name" /></CommandDialog>);};Mount a new dialog instance per operation and unmount on close.
idis stable during editing and fresh for the next registration; a required Guid is not automatically filled by the proxy.The field accessor
i => i.namerefers to the generated type. If you rename that property on the backend, regenerate the proxy and type-check the frontend to find references that need updating. -
Supply values the user should not type through
initialValues. For a book, that includes the parent author identifier. Do not supply required values only inonBeforeExecute: it runs after validation, too late to make an invalid form valid.This alternative form fragment needs the book dialog’s imports and per-operation
bookIdstate. KeepauthorIdas the generatedGuid, not a string:<CommandDialog<AddBook> command={AddBook} title="Add book" initialValues={{ authorId, bookId }}><InputTextField<AddBook> value={i => i.title} title="Title" /></CommandDialog> -
Open the dialog through the tutorial’s dialog composition and submit a valid name. Supported rules extracted from the command and its value types run client-side. Async, custom, and state-dependent rules still run on the server; returned field messages appear in the dialog.
Check the result
Section titled “Check the result”With the tutorial’s required-name rule in place, leave the name empty and confirm that submission is disabled. Enter a valid, unused name and submit: confirm should stay disabled while the request runs, and a successful registration should close the dialog. Check the stored author or author query as well; closing a dialog alone does not prove that the intended data was written.
With the tutorial’s duplicate-name validation in place, submit an existing name. The dialog should stay open and show the name-field feedback. If a required value leaves confirm disabled, check initialValues before adding a submit callback.
For a plain button or custom flow without a dialog, call the proxy’s execute() method. Check the returned CommandResult.isSuccess before reading response; the Command Result guide covers failure handling.
See also
Section titled “See also”- Dialogs —
CommandDialog,Dialog, and the form-field components. - Command Result — handling the result in code.
- Validate a command — the rules that surface in the form.