Sub topic 8
Handling events and passing data
This chapter assumes you can read an LWC template and JavaScript class, and that you know what a base component like lightning-input is. It covers how base components send data back to your code and how your code sends data into them.
The data flow model
Data flows in two directions, and each direction uses a different mechanism.
- Down: a parent sets attributes on a base component, such as
value={searchKey}. - Up: the base component fires a DOM event, and your handler reads the details.
Base components do not change your JavaScript fields for you. If a user types in a lightning-input, the input shows the new text, but searchKey stays the same until your onchange handler assigns it.
Listening for events
Add a handler with an on prefix and the event name in lowercase. The handler is a method on your class, referenced in braces without parentheses.
<template>
<lightning-input
label="Name"
value={searchKey}
onchange={handleChange}
></lightning-input>
<p>You typed: {searchKey}</p>
</template>import { LightningElement } from 'lwc';
export default class LiveSearch extends LightningElement {
searchKey = '';
handleChange(event) {
this.searchKey = event.target.value;
}
}Fields in an LWC class are reactive. Assigning a new value to searchKey re-renders the template, so the paragraph updates on every change. Because the field is also bound to value, the input and the class stay in sync.
Where the data lives in the event
Different base components put their data in different places. Check the component's reference page before assuming event.target.value.
| Component | Event | Where the data is |
|---|---|---|
| `lightning-input` | `change` | `event.target.value` (or `event.target.checked` for a checkbox) |
| `lightning-combobox` | `change` | `event.detail.value` |
| `lightning-button` | `click` | No payload. Use `data-` attributes to identify it. |
| `lightning-datatable` | `rowselection` | `event.detail.selectedRows` |
Notice that the combobox uses event.detail. Base components that fire custom events place their payload in detail, while standard DOM events on native-like inputs expose values through target.
Identifying which element fired
When several elements share one handler, add a data- attribute and read it from event.target.dataset. Use currentTarget when the listener is attached to the element whose data you want.
<template>
<lightning-input data-field="firstName" label="First name" onchange={handleChange}></lightning-input>
<lightning-input data-field="lastName" label="Last name" onchange={handleChange}></lightning-input>
</template>import { LightningElement } from 'lwc';
export default class NameFields extends LightningElement {
firstName = '';
lastName = '';
handleChange(event) {
const field = event.target.dataset.field;
this[field] = event.target.value;
}
}Common errors
- Writing
onchange={handleChange()}. Pass the method itself, not the result of calling it. - Setting
valuebut never handlingchange. The field never updates, and the next re-render may reset what the user typed. - Reading
event.target.valueon a component that puts its data inevent.detail. - Mutating an object in place and expecting a re-render. Reassign the field with a new object or array instead.
- Using uppercase or camelCase in the event attribute, such as
onChange. Use lowercase.
Exercise
Exercise
A lightning-input has value={searchKey} but no onchange handler. The user types "abc". What does searchKey contain, and why?
Show answer
It still contains its original value. Base components do not write back to your fields. The input displays the typed text, but only an onchange handler that assigns event.target.value to this.searchKey updates the class field.
Exercise
A lightning-combobox handler uses event.target.value and works, but a teammate says to use event.detail.value. Which is the documented place for the combobox payload, and why prefer it?
Show answer
event.detail.value is the documented payload for the combobox change event. Following the documented location is safer, because it does not depend on the component's internal element state at the time the handler runs.