# How do you expose public properties and methods to parent components using the @api decorator?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, Decorators, Public Properties

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-you-expose-public-properties-and-methods-to-parent-components-using-the-api-decorator

## Answer

**Basics:** Components are black boxes. Anything not decorated with `@api` is private. Public properties let a parent configure a child (`<c-card title="Hello">`), and public methods let a parent command a child (`this.refs.card.reset()`). Public properties also become configurable in Lightning App Builder when listed in the meta XML.

**How it works:**
1. Child declares `@api title;`. In parent HTML camelCase `recordId` becomes the attribute `record-id`.
2. The parent assigns the value; the child re-renders when the value changes.
3. Parent code reaches methods by getting the element via `this.template.querySelector('c-child')` (or `lwc:ref` with `this.refs`) and calling `el.method()`.
4. For validation or derived behavior, use a getter and setter pair; decorate only the first of the pair with `@api`.
5. For App Builder, add `<property name="title" type="String"/>` inside `targetConfig` in the meta file.

**Advanced / deep dive:** Data flows down through properties and up through events, which keeps components predictable. A child that mutates an `@api` object it received would silently alter the parent's data, so the framework warns or throws; clone before editing. Setters run before `connectedCallback` for initial values and again on each change, so they must tolerate `undefined` and run before other properties are available. Public methods are an escape hatch; prefer property-driven design where possible, because methods couple the parent to child internals and break if the child has not rendered yet. Property names cannot start with `on`, `data` or `aria`, and cannot be reserved words like `slot` or `part`. A boolean `@api` property whose default is `true` cannot be turned off through an HTML attribute, because the attribute's presence means true; default booleans to `false`.

**Code / config example:**
```javascript
// child: statusBadge.js
import { LightningElement, api } from 'lwc';
export default class StatusBadge extends LightningElement {
    _status;
    @api
    get status() { return this._status; }
    set status(v) { this._status = (v || 'new').toLowerCase(); }

    @api reset() { this._status = 'new'; }   // public method
}
```
```html
<!-- parent.html -->
<c-status-badge lwc:ref="badge" status={caseStatus}></c-status-badge>
```
```javascript
// parent.js
handleReset() { this.refs.badge.reset(); }
```

**Hidden gotchas & must-remember points:**
- Do not decorate both the getter and the setter with `@api`; that is a compile error.
- A child must not assign to a received `@api` value from outside its own setter; clone objects first.
- `@api` is required on `recordId` and `objectApiName` to get them auto-populated on record pages.
- Calling a child's public method before it renders throws or does nothing; call after `renderedCallback` or on a user action.
- Property-level `@api` values passed as attributes are always strings unless bound with `{}` expressions.
- Properties named with `data-`, `aria-` or `on` prefixes are rejected.

**Best practice:** Keep the public API small, immutable-in, event-out; expose a method only for imperative actions such as `focus()` or `validate()`.

---
Source: ForceChallenges · last updated 2026-11-02
