Sub topic 10
Exposing methods with @api
This chapter assumes you can read a basic Lightning web component (LWC), know how a parent template includes a child, and understand that @api makes a field a public property. Here you will learn how @api also exposes methods, so a parent can tell a child to do something.
Why expose a method
Public properties pass data down from parent to child. Some jobs are actions, not data: move focus to an input, reset a form, start an animation. A property is a poor fit for these, because you would have to set a flag and watch for it in the child.
A method decorated with @api is part of the child's public interface. The parent calls it like any JavaScript method on the element.
Declaring a public method
Put @api directly above the method in the child class.
import { LightningElement, api } from 'lwc';
export default class SearchBox extends LightningElement {
@api
focusInput() {
const input = this.template.querySelector('input');
if (input) {
input.focus();
}
}
}The method looks up the input element in its own template. The if check guards against the element not being rendered yet. Without @api, the method would stay private, and the parent could not call it.
Calling the method from the parent
The parent finds the child element in its own template, then calls the method on it.
import { LightningElement } from 'lwc';
export default class SearchPage extends LightningElement {
handleReset() {
const box = this.template.querySelector('c-search-box');
if (box) {
box.focusInput();
}
}
}The selector uses the child's tag name, c-search-box. The component searchBox in the c namespace becomes c-search-box, with camelCase written in kebab-case. The if check handles a child that is not in the DOM, for example because an lwc:if directive removed it.
Timing: the child must exist and be rendered
A parent can only call a method on a child that is in the DOM. If you call it from the parent's connectedCallback, the child has not rendered yet, so querySelector returns null. Call child methods from event handlers, or from renderedCallback when the child is known to be present.
Methods, properties, and rerendering
Calling an @api method does not rerender anything by itself. A rerender happens only when the method changes a reactive field that the template uses. In focusInput, no field changes, so no rerender occurs. Focus moves directly in the DOM.
A method that updates state works the same way as any other code in the class.
import { LightningElement, api } from 'lwc';
export default class Tally extends LightningElement {
count = 0;
@api
reset() {
this.count = 0;
}
increment() {
this.count += 1;
}
}Here reset() reassigns count, so a template that reads {count} rerenders when the parent calls it.
Methods versus properties
Choose by the kind of thing you are exposing.
| Need | Use | Example |
|---|---|---|
| Pass a value down | `@api` property | `heading`, `expanded` |
| Transform or validate a value | `@api` getter and setter | `amount` |
| Trigger an action | `@api` method | `focusInput()`, `reset()` |
A method can also return a value, such as @api getSelectedIds(). Use that when the parent needs to read state the child computed, and a property does not fit.
Keep the public surface small
Every @api method is a contract. Other components may depend on its name and arguments, so renaming it later breaks them. Expose only what a parent truly needs. Prefer a public property when the parent only passes data, and an event when the child needs to tell the parent something. Methods are for commands that flow down.
Exercise
A parent calls this.template.querySelector('c-search-box').focusInput() inside its own connectedCallback, and the input never gets focus. What is the likely cause, and where could you move the call?
Show answer
In connectedCallback, the parent's template has not rendered, so querySelector returns null (or the child is not in the DOM yet) and the call does not happen. Move it to an event handler such as a button click, or to renderedCallback with a null check, once the child exists.
Exercise
Why does the focusInput method not cause the component to rerender?
Show answer
It does not change any reactive field that the template reads. It only calls focus() on a DOM element, which changes browser focus and nothing in the component's state.