Sub topic 9
Handling input and one-way data flow
This chapter assumes you can create an LWC bundle (HTML, JS, and meta XML files). It also assumes you know how {property} binding and getters work, and that reassigning a class field re-renders the template.
One-way data flow
LWC binds data in one direction. The class field flows into the template, and the template does not write back to the field. If a user types in an input, the field keeps its old value until your code changes it.
There is no two-way binding syntax, so you wire up both directions yourself:
- Bind the field to the input's
value. This is the field-to-template direction. - Listen for the
changeevent. This is the template-to-field direction. - In the handler, assign the new value to the field.
- The assignment triggers a re-render, and the template shows the new state.
The template
The template binds the input's value, attaches a handler, and shows a message and a CSS class that both depend on the current name.
<!-- greeter.html -->
<template>
<lightning-input label="Your name" value={name} onchange={handleChange}></lightning-input>
<p class={messageClass}>{message}</p>
</template>Handler attributes start with on followed by the event name. The value in braces is a reference to a method on the class. You write onchange={handleChange}, not onchange={handleChange()}, and you can't pass arguments here.
The class
// greeter.js
import { LightningElement } from 'lwc';
export default class Greeter extends LightningElement {
name = '';
handleChange(event) {
this.name = event.target.value;
}
get message() {
return this.name ? `Hello, ${this.name}!` : 'Please enter your name.';
}
get messageClass() {
return this.name ? 'slds-text-color_success' : 'slds-text-color_weak';
}
}Here is what happens when the user types "Ada" and leaves the field:
- The
changeevent fires andhandleChangeruns. event.target.valueholds the text in the input, and the handler assigns it tothis.name.- The field changed, so the component re-renders.
- The
messagegetter returnsHello, Ada!, andmessageClassreturns the success class.
The getters need no extra code. They read this.name, so they return fresh values on every render.
Reading the event
Inside a handler, event.target is the element that fired the event. For lightning-input, the current text is event.target.value. Native <input> elements work the same way. Some base components expose the value differently. A lightning-checkbox-group, for example, puts the selected values in event.detail.value. Check the component's documentation when event.target.value is undefined.
When the field is not updated
If the handler doesn't assign the new value, the field keeps its old one. For a lightning-input, the component re-renders from the field on the next render, so the text can revert to the stale value. This is the most common beginner bug with input handling. It is the one-way rule at work, not a framework fault.
// Bug: the handler never writes the value back
handleChange(event) {
console.log(event.target.value);
}Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Handler never runs | Wrong event name, such as `onchanged` | Use the event the component fires, such as `onchange` |
| `Cannot read properties of undefined` in handler | Method used `this` after being passed as a plain function | Declare it as a class method and bind it with `onchange={handleChange}` |
| Typed text disappears | The handler doesn't assign to the field | Assign `event.target.value` to the field |
| `onchange={handleChange()}` rejected | Parentheses in the template | Remove the parentheses |
Choosing between change and input events
change fires when the user commits a value, usually on blur or Enter. If you want to update on every keystroke, listen for onchange on lightning-input, which fires as the user types for text fields. For a native <input>, use oninput for per-keystroke updates and onchange for committed values.
Exercise
A component has <lightning-input value={city}></lightning-input> and a city field set to 'Oslo', but no onchange handler. The user types "Bergen" into the input. What does the class field city contain afterward, and what must you add so the field follows the input?
Show answer
The field still contains 'Oslo', because LWC binds one way and the template never writes back to the class. Add onchange={handleCityChange} to the input and a method handleCityChange(event) { this.city = event.target.value; } to the class.
Exercise
Why does the Greeter component not need any code to update message and messageClass after handleChange runs?
Show answer
The handler reassigns the name field, which triggers a re-render. During the render, the template reads the message and messageClass getters, which read this.name and return values based on the new name.