Sub topic 8
Controlling Next, Back, Pause, and Finish
This chapter assumes you can build a basic LWC with @api properties, expose it to Flow Builder with the lightning__FlowScreen target, and send values back with FlowAttributeChangeEvent. Here you will control the flow's navigation buttons from inside your component.
How the flow tells your component what is allowed
A screen flow shows a footer with navigation buttons. Which buttons appear depends on the flow's structure and on the screen's settings. For example, the first screen has no Back button, and the last screen shows Finish instead of Next.
The flow passes this information to your component through a reserved property named availableActions. It is an array of strings. Possible values are BACK, NEXT, PAUSE, and FINISH. Declare it with @api and the flow fills it in automatically. You do not add it to the meta.xml file.
@api availableActions = [];Always read this array before you navigate. If you dispatch an event for an action the flow does not allow, nothing happens, and the user sees a button that does not work.
Navigation events
The lightning/flowSupport module provides one event class for each action.
| Action | Event class | Required in `availableActions` |
|---|---|---|
| Next | `FlowNavigationNextEvent` | `NEXT` |
| Back | `FlowNavigationBackEvent` | `BACK` |
| Pause | `FlowNavigationPauseEvent` | `PAUSE` |
| Finish | `FlowNavigationFinishEvent` | `FINISH` |
You create the event with no arguments and dispatch it from your component.
A component with its own buttons
You might hide the standard footer and draw your own buttons. This example shows Back and a primary button that is labeled Next or Finish, depending on what the flow allows.
import { LightningElement, api } from 'lwc';
import {
FlowNavigationBackEvent,
FlowNavigationNextEvent,
FlowNavigationFinishEvent
} from 'lightning/flowSupport';
export default class StepButtons extends LightningElement {
@api availableActions = [];
get canGoBack() {
return this.availableActions.includes('BACK');
}
get canGoForward() {
return (
this.availableActions.includes('NEXT') ||
this.availableActions.includes('FINISH')
);
}
get forwardLabel() {
return this.availableActions.includes('FINISH') ? 'Finish' : 'Next';
}
handleBack() {
if (this.canGoBack) {
this.dispatchEvent(new FlowNavigationBackEvent());
}
}
handleForward() {
if (this.availableActions.includes('FINISH')) {
this.dispatchEvent(new FlowNavigationFinishEvent());
} else if (this.availableActions.includes('NEXT')) {
this.dispatchEvent(new FlowNavigationNextEvent());
}
}
}<template>
<div class="slds-m-top_medium">
<lightning-button
if:true={canGoBack}
label="Back"
onclick={handleBack}>
</lightning-button>
<lightning-button
if:true={canGoForward}
variant="brand"
label={forwardLabel}
onclick={handleForward}>
</lightning-button>
</div>
</template>What happens when you navigate
Navigation events work with the validation you set up earlier. When you dispatch Next or Finish, the flow calls validate() on every component on the screen. If any component returns isValid: false, the flow stays on the screen and shows the error message. Back and Pause do not run validation.
Common mistakes
- Ignoring
availableActions. Showing a Back button on the first screen leads to a button that does nothing. - Dispatching Next before saving values. Dispatch
FlowAttributeChangeEventfor every changed output first, then navigate. Otherwise the next element can read stale values. - Hiding the standard footer without a replacement. The screen's footer settings are in Flow Builder. If you hide the footer, your component must provide every button the user needs.
- Assuming Pause is always available. The flow admin must enable Pause on the screen, and the flow must be able to be resumed. Check for
PAUSEbefore you offer it.
Exercise
A component dispatches FlowNavigationNextEvent and the user stays on the same screen with an error message. What is the most likely cause, and where should you look?
Show answer
One of the components on the screen returned isValid: false from its validate() method, so the flow blocked navigation and displayed the errorMessage. Check the validate() logic in the components on that screen, and confirm that the component's value was reported with FlowAttributeChangeEvent before navigating.
Exercise
Why should a component check availableActions.includes('FINISH') before dispatching FlowNavigationFinishEvent?
Show answer
The flow only allows Finish on the last screen. If the action is not in availableActions, the event has no effect and the user sees a button that does nothing. Checking the array lets the component show only the buttons that work on the current screen.