Sub topic 1
How screen flows host Lightning web components
A screen flow can show a Lightning web component (LWC) on any screen. This chapter explains how the flow runtime hosts your component, what lifecycle it goes through, and how data moves in and out. Later chapters cover the meta.xml, properties, validation, and navigation in detail.
Prerequisites
This chapter assumes you already know the following:
- Lightning web component basics: templates, JavaScript classes, and
@apiproperties. - Flow Builder basics: screen flows, variables, and screen elements.
- How to deploy a component with Salesforce DX or the Developer Console.
- Custom events in JavaScript, including
dispatchEventandCustomEvent.
What a screen flow is
A screen flow is a flow that interacts with a user. It is made of elements that run in order. Some elements show a screen, some read or write records, and some branch on conditions. The flow stores its data in resources such as variables, collections, and formulas.
A screen is one step the user sees. It holds screen components. These can be standard components such as Text, Number, and Picklist. They can also be custom components, which are Lightning web components you wrote.
Your component is one screen component among others. The flow, not your component, owns the data and decides what happens next.
Who owns what
The relationship between the flow and your component is a contract. Each side has a clear job.
| Responsibility | Flow runtime | Your component |
|---|---|---|
| Stores the authoritative data | Yes. Values live in flow variables. | No. It holds a working copy. |
| Creates the component | Yes, when the screen is shown. | No. |
| Supplies input values | Yes, by setting `@api` properties. | Receives them. |
| Receives output values | Yes, after a change event. | Sends them with `FlowAttributeChangeEvent`. |
| Checks validity | Calls `validate()` on Next or Finish. | Implements `validate()`. |
| Moves between screens | Yes. | Requests it with navigation events. |
A common mistake is to treat the component as the source of truth. The flow variable is the source of truth. If your component changes a value but never tells the flow, the flow still has the old value.
The lifecycle of a component on a screen
The flow creates a new instance of your component each time its screen is displayed. It does not keep one instance alive across the whole flow.
- The flow reaches a screen element and builds the screen.
- For each custom component, the runtime creates an instance and sets the
@apiproperties that you configured in Flow Builder. - The standard LWC lifecycle runs:
constructor, thenconnectedCallback, thenrender, thenrenderedCallback. - The user interacts with the screen. Your component sends changes to the flow as events.
- The user clicks Next, Previous, Pause, or Finish. The runtime calls
validate()on components when the action requires validation. - If validation passes, the runtime leaves the screen and removes your component. The instance is destroyed and
disconnectedCallbackruns. - If the user comes back to the screen, the runtime builds a new instance and sets the properties again from the current flow values.
Because the instance is recreated, any state held only in a JavaScript field is lost when the user leaves the screen. If you need a value to survive, send it to the flow as an output. On return, the flow gives it back to you as an input.
How data moves
Data moves in two directions through two different mechanisms.
Flow to component
The flow sets your @api properties. You configure these values in Flow Builder when you add the component to a screen. A value can be a literal, or a reference to a flow resource such as {!varAccountName}.
Treat these properties as read-only. The runtime may hand you an object that is frozen or shared. Do not modify it in place. Copy it first, for example with the spread operator, and change the copy.
Component to flow
Your component sends values back by dispatching a FlowAttributeChangeEvent. It takes the property name and the new value. The flow stores the value in whatever resource you mapped the output to in Flow Builder.
import { LightningElement, api } from 'lwc';
import { FlowAttributeChangeEvent } from 'lightning/flowSupport';
export default class NameEcho extends LightningElement {
@api name = '';
handleChange(event) {
this.name = event.detail.value;
this.dispatchEvent(new FlowAttributeChangeEvent('name', this.name));
}
}Setting this.name updates what the user sees. The dispatchEvent call updates the flow. You need both.
A property can be input only, output only, or both. A property that is both is the common case for form fields. The flow supplies the starting value and receives the edited value.
A first end-to-end example
The following component shows a greeting and lets the user edit the name. It is small, but it uses every part of the contract. Create a bundle named nameEcho with these three files.
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<isExposed>true</isExposed>
<masterLabel>Name Echo</masterLabel>
<targets>
<target>lightning__FlowScreen</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__FlowScreen">
<property name="name" type="String" label="Name"/>
</targetConfig>
</targetConfigs>
</LightningComponentBundle><template>
<lightning-input
type="text"
label="Your name"
value={name}
onchange={handleChange}>
</lightning-input>
<p>Hello, {name}</p>
</template>The JavaScript file is the NameEcho class shown in the previous section.
To try it, build a screen flow with a text variable named varName. Add a screen, drag Name Echo onto it, and set its Name to {!varName}. Add a second screen with a Display Text element that shows {!varName}. Run the flow, type a name, and click Next. The second screen shows what you typed, which proves the output reached the flow.
What the component cannot do
The hosting model sets some limits.
- The component cannot read other flow variables on its own. It only sees the properties you map to it.
- The component cannot write to a flow variable directly. It must send a change event.
- The component does not control the screen lifecycle. The runtime decides when to create and remove it.
- The component cannot assume it is the only thing on the screen. Other components, and the standard Next and Previous buttons, share the page.
Edge cases to know early
- Multiple instances. The same component can appear on a screen twice. Each instance has its own state and its own mapped properties. Do not store state in module-level variables, because the instances would share them.
- Reactivity of inputs. If another screen component changes a flow variable that you read, the runtime can pass your component a new value on the same screen. Write your setters so a repeated assignment is safe.
- Undefined and null. An unset input can arrive as
undefinedornull. Check for both before you call methods on a value. - Type conversion. The type you declare in meta.xml controls what the flow accepts. A
Stringproperty cannot be mapped to a number variable in Flow Builder. - Default values. A default in the JavaScript class and a default in meta.xml can disagree. The value set by the flow takes precedence once the component is created.
- Navigation buttons. Standard Next and Previous buttons sit outside your component. Pressing them triggers validation and the move to another screen. Your component can also request navigation itself, which a later chapter covers.
Where this fits in the larger picture
Screen flows are one of several places a component can run, and the lightning__FlowScreen target is what opts yours in. The next chapter covers that target. After it, you will declare typed properties, receive inputs, and send outputs. Validation and navigation come after wiring the component in the builder.
Exercise
A user types a value into your component, clicks Next, then clicks Previous to return. The field is empty even though the user typed something. List two likely causes and the fix for each.
Show answer
First, the component updated only its own JavaScript field and never dispatched a FlowAttributeChangeEvent, so the flow never stored the value. Fix: dispatch the event on each change. Second, the property was not mapped to a flow variable in Flow Builder, or the mapping was input only. Fix: map the property to a variable and make sure it is an output as well. The runtime creates a new instance on return, so it can only restore what the flow stored.
Exercise
Why should a component avoid keeping data in a module-level variable, and where should that data go instead?
Show answer
Module-level variables are shared by every instance of the component, so two copies on one screen would overwrite each other. Keep per-instance data in class fields. Keep data that must survive leaving the screen in a flow variable by sending it as an output.