Skip to content

Commands

To open an account from React, fill in the generated command’s properties and execute it from a component’s event handler. Arc generates the HTTP surface from model-bound commands, and in C# also from discovered controller actions. Either style can use validation and authorization; neither requires Chronicle or event sourcing.

A command represents a requested change. Its generated proxy gives the component typed input, change tracking, and an execution result without a separate HTTP client. The .use() hook keeps that command available to the component as it renders.

Use a command for the write and a query to read the resulting state. The hook does not provide a complete form or disable the submit button during a request: manage execution state in the component, or use CommandForm for a declarative form.

Start with the backend declarations used to generate the proxy below:

public record OpenDebitAccount(AccountId AccountId, AccountName Name, CustomerId Owner);

View C# snippet source on GitHub

The backend tabs reach the shared generated-command runtime from different starting points. The C# record is a payload data transfer object (DTO): it requires an already-defined controller endpoint accepting it, because a record alone is not enough for proxy discovery. The JVM has no controller-based discovery, so the model-bound @Command is the endpoint. The TypeScript backend generates from decorated @command() classes with a handle() method; see its generator setup. For a complete model-bound C# example, follow getting started.

In the React consumer, call the hook inside a component and execute from its event handler. This example assumes string-backed AccountId and CustomerId; use Fundamentals Guid values instead if the generated model maps Guid-backed concepts.

import { OpenDebitAccount } from './generated/commands';
export function OpenAccount() {
const [command] = OpenDebitAccount.use({
accountId: 'a23edccc-6cb5-44fd-a7a7-7563716fb080',
name: 'My Account',
owner: '84cda809-9201-4d8c-8589-0be37c6e3f18'
});
async function open() {
const result = await command.execute();
console.log(result.isSuccess ? 'Opened.' : 'Could not open account.');
}
return <button onClick={() => void open()}>Open account</button>;
}

The initial values describe the account to open; execute() submits them and returns the result. The example logs a summary. A form should also show the relevant validation or authorization feedback and prevent unintended repeated submissions while a request is pending.

Command fetches include httpHeadersCallback headers configured in Arc. Supply Authorization or application headers there; cookies are browser-managed, not configurable Cookie request headers.

The generated class follows the backend command’s input contract and adds validation support, change tracking, and .use(). On C#, the proxy generator discovers HTTP POST actions from the compiled assembly in a configured post-build step. It does not run as a compiler source generator.

Regenerate after changing the backend contract, then type-check the frontend to find callers that need updating. Consume generated proxies connects the backend-specific setup to React usage.

Await execute() and inspect its CommandResult before treating the change as successful. Authorization and validation failures need different feedback from a successful response:

const result = await command.execute();
if (result.isSuccess) {
console.log('Success!', result.response);
} else {
if (!result.isAuthorized) {
// Handle authorization failure
}
if (!result.isValid) {
// Handle validation errors
}
}

These checks distinguish authorization and validation failures; they do not cover every unsuccessful result. Use the Command Result documentation for exception information and the complete result contract. Continue with React usage to manage the command through the component’s lifecycle.

Topic Description
React Usage Using the .use() hook in React components (recommended).
Data Binding Binding to command properties and managing initial values for change tracking.
Validation Pre-flight validation, progressive validation, and error handling.
Command Scope Tracking changes across multiple commands in composite UIs.
Imperative Usage Advanced direct instantiation for non-React scenarios.