Sub topic 11
Design guidelines and common pitfalls
This chapter assumes you can build a basic flow screen component. You should know @api properties, meta.xml targets, FlowAttributeChangeEvent, and validate(). It collects the rules that keep those components reliable and the mistakes that cause most support tickets.
Design guidelines
Follow these rules when you design a component for a screen flow.
- Keep the component focused. One component should do one job, such as picking a quantity. Flow Builder handles branching, so don't build multi-step logic inside the component.
- Let the flow own the data. The component reads inputs and reports outputs. It should not hold the only copy of a value the flow needs.
- Expose only what an admin needs. Each
propertyelement adds something to configure. Give every one a clearlabelanddescription. Userole="inputOnly"orrole="outputOnly"when a property goes one way. - Give inputs sensible defaults. A
defaultinmeta.xmlmeans the component works the moment it is dropped on a screen. - Respect flow navigation. Check
availableActionsbefore you fire a navigation event. Put validation invalidate()instead of hiding buttons.
How a value travels
The flow passes inputs in when the screen renders. The component sends outputs back as the user edits them.
Common pitfalls
| Pitfall | What happens | Fix |
|---|---|---|
| Changing an `@api` value without an event | The component shows the new value, but the flow keeps the old one | Dispatch `FlowAttributeChangeEvent` after every change |
| Mutating an input object or collection | Lightning Web Security or the read-only proxy throws an error, or the change is lost | Copy the data, change the copy, and send it back as an output |
| Forgetting `validate()` | Users move on with empty or bad data | Return `{ isValid, errorMessage }` and check required values |
| Dispatching a navigation event that is not allowed | Nothing happens | Check `availableActions` first |
| Renaming a property after release | Existing flows lose their mapping | Add new properties and keep the old names until flows are updated |
| Reading inputs in `constructor()` | Values are not set yet | Use `connectedCallback()` or a getter |
Avoid mutating read-only inputs
Values that arrive through @api are read-only inside the child. Copy a collection before you edit it.
import { LightningElement, api } from 'lwc';
import { FlowAttributeChangeEvent } from 'lightning/flowSupport';
export default class RecordPicker extends LightningElement {
@api records = [];
@api selectedRecord;
handleSelect(event) {
const id = event.currentTarget.dataset.id;
const match = this.records.find((record) => record.Id === id);
// Send a copy so the flow's own collection stays untouched.
this.selectedRecord = match ? { ...match } : undefined;
this.dispatchEvent(
new FlowAttributeChangeEvent('selectedRecord', this.selectedRecord)
);
}
}A pre-release checklist
- Every property has a label and the right
role. - Every user change fires
FlowAttributeChangeEvent. validate()covers each required value and returns a useful message.- Navigation code checks
availableActions. - No input is mutated in place.
- You tested the component with empty, default, and invalid values.
Exercise
A component lets a user edit a value passed in as @api amount. The screen shows the new amount, but a later flow element still sees the old amount. What is missing, and how do you fix it?
Show answer
The component changed its own property but never told the flow. Dispatch new FlowAttributeChangeEvent('amount', this.amount) after each change. Also make sure amount is declared as a property in meta.xml and is not marked role="inputOnly".
Exercise
Your component calls .push() on an @api collection to add an item, and the call throws an error. What should you do instead?
Show answer
Treat the input as read-only. Copy the collection, for example const updated = [...this.items, newItem];. Then send updated to the flow as an output with FlowAttributeChangeEvent.