Sub topic 11
Design guidelines and common pitfalls
This chapter assumes you can already build a flow screen component. You should know the lightning__FlowScreen target, @api properties, FlowAttributeChangeEvent, validate(), and the availableActions navigation events. It collects the design rules and failure modes that most often cause trouble in production flows.
Design guidelines
Treat the component as a form field, not an app
A flow screen component is one element on a screen that an admin arranges. It doesn't own the page. Design it around a narrow contract: it receives a few inputs, shows one focused piece of UI, and returns a few outputs. Components that try to manage the whole screen, such as custom Next buttons beside the flow footer, are harder to reuse and easier to break.
Prefer a standard screen component when one exists. Build a custom component only when the standard components can't do the job.
Keep the contract small and well named
Every property in meta.xml becomes something an admin must understand. Follow these rules:
- Give every property a
labeland adescription. Admins see these in Flow Builder, not your JavaScript. - Use
role="inputOnly"orrole="outputOnly"when a property goes only one way. Otherwise Flow Builder shows both input and output options. - Provide a
defaultfor inputs that have a sensible fallback. - Keep the
nameinmeta.xmlidentical to the JavaScript property name, including case.FlowAttributeChangeEventalso uses this name.
Make the component stateless across screens
Flow creates a new instance of your component each time its screen is displayed, including when the user clicks Back and returns. Anything you keep only in component fields is lost. The flow's variables are the only memory.
To make state survive navigation:
- Dispatch a
FlowAttributeChangeEventevery time a value changes. - Have the admin map each output to a flow variable.
- Map the same variable to the matching input, so the component restores its state when it reappears.
Validate in the component, not by hiding buttons
Use validate() to block forward navigation. Don't disable the footer buttons or hide them with CSS. validate() shows the error message in the flow's standard error area, and it works the same on every runtime.
Use navigation events sparingly
Dispatch FlowNavigationNextEvent and its siblings only for deliberate actions, such as a "Select" button that should advance immediately. Always check availableActions first. The flow ignores or mishandles an action that isn't allowed on the current screen, such as Back on the first screen.
Common pitfalls
Mutating read-only inputs
Objects and arrays that arrive from the flow are read-only. If you call push() on an array or assign to a field of an input record, the component throws an error. The error often appears only when real data is passed, not when the property is empty.
Copy the value when it arrives. The component below accepts a text collection, adds to a private copy, and reports the new collection back.
import { LightningElement, api } from 'lwc';
import { FlowAttributeChangeEvent } from 'lightning/flowSupport';
export default class TagEditor extends LightningElement {
_tags = [];
newTag = '';
@api
get tags() {
return this._tags;
}
set tags(value) {
// Copy so we never modify the read-only array supplied by the flow.
this._tags = Array.isArray(value) ? [...value] : [];
}
handleInput(event) {
this.newTag = event.target.value;
}
handleAdd() {
const tag = this.newTag.trim();
if (!tag) {
return;
}
this._tags = [...this._tags, tag];
this.newTag = '';
this.dispatchEvent(new FlowAttributeChangeEvent('tags', this._tags));
}
}<template>
<lightning-input label="New tag" value={newTag} onchange={handleInput}></lightning-input>
<lightning-button label="Add tag" onclick={handleAdd}></lightning-button>
<ul>
<template for:each={tags} for:item="tag">
<li key={tag}>{tag}</li>
</template>
</ul>
</template>The matching meta.xml property is <property name="tags" type="String[]" label="Tags"/>. Note that _tags is replaced with a new array on each change, never edited in place. This also keeps the template re-rendering reliably.
Forgetting to dispatch the output event
Changing this.quantity updates your component only. The flow variable changes only when you dispatch FlowAttributeChangeEvent. This is the most common reason an admin reports that "the output is always empty."
Dispatch the event in every code path that changes an output, including paths that clear a value. Send undefined or null to clear it, not nothing.
Assuming values arrive as the right type
Values that an admin types into a property field in Flow Builder may reach your component as strings, even if you declared Integer. Values from flow variables normally match the declared type. Coerce defensively in the setter or the code that uses the value, for example with Number() or parseInt(), and handle NaN.
Relying on constructor or connectedCallback timing
The flow sets your @api properties before connectedCallback() runs, but it can set them again later if a value changes. Don't read an input once and cache it forever. Use a setter or a getter that reflects the current value. Don't start imperative Apex calls in connectedCallback() that depend on inputs which may still change.
Validating at the wrong time
validate() runs when the user tries to move forward. It doesn't run when the user types. If you want inline feedback while typing, use the standard setCustomValidity() and reportValidity() methods on the inner input as well. Treat validate() as the gate and inline feedback as a convenience.
Also keep validate() synchronous and fast. It must return an object right away. If you need server data, fetch it earlier and store the result in a field that validate() reads.
Navigating without checking availableActions
Calling new FlowNavigationNextEvent() on a screen where Next isn't allowed does nothing useful. Your own button will then appear broken. Hide or disable your control based on availableActions, as in the navigation chapter.
Skipping the redeploy and refresh cycle
Flow Builder caches component metadata. After you change meta.xml, such as adding a property or renaming one, deploy it and reload Flow Builder. Renaming or removing a property that existing flows already use can leave those flows with stale mappings, so treat property names as a public API. Add new properties instead of renaming old ones.
Ignoring runtime differences
A component that works in Flow Builder's debug run can behave differently in an Experience Cloud site or in a flow embedded elsewhere. Styling, available navigation actions, and user permissions can differ. Test the component in every place where the flow will run, using users with realistic profiles, not only an administrator.
Quick reference
| Symptom | Likely cause | Fix |
|---|---|---|
| Error about a read-only object or array | Mutating a value supplied by the flow | Copy it in the setter with spread syntax |
| Output variable stays empty | No `FlowAttributeChangeEvent` dispatched | Dispatch on every change, including clears |
| Value lost after clicking Back | State kept only in component fields | Map outputs to variables and feed them back as inputs |
| New property missing in Flow Builder | Old metadata cached or not deployed | Redeploy and reload Flow Builder |
| Next button does nothing from your code | Action not in `availableActions` | Check `availableActions` before dispatching |
| Validation message never appears | `validate()` missing `@api` or wrong return shape | Return `{ isValid, errorMessage }` from an `@api` method |
A pre-release check
The diagram shows the sequence worth following before you hand a component to admins.
Step E catches the stateless-component problem. Click Next, click Back, and confirm the component shows what the user entered.
Exercises
Exercise
A component receives a record collection through @api records and calls this.records.push(newRecord). It fails with an error about a read-only value. What is the cause, and what is the fix?
Show answer
Values passed from the flow are read-only, so in-place changes such as push() fail. Copy the collection in a setter, for example this._records = [...value]. Then replace the array with a new one when it changes, and dispatch a FlowAttributeChangeEvent so the flow receives the new collection.
Exercise
A user enters a value, clicks Next, then clicks Back, and the field is empty. The component dispatches a FlowAttributeChangeEvent for the value. Name two things to check in the flow.
Show answer
First, check that the component's output is mapped to a flow variable. Second, check that the same variable is mapped to the component's matching input, so the new instance receives the value when the screen is shown again. The component also needs a setter or getter that uses the supplied input to restore its state.
Exercise
Why is validate() better than disabling the Next button when a field is invalid?
Show answer
validate() blocks navigation and shows the error message in the flow's own error area. It works the same on every runtime and gives the user a reason. A disabled or hidden button gives no explanation and can drift out of sync with the real validation rules.