Skip to main content

Overview

The @api decorator marks a property as public, making it accessible from parent components. Public properties enable parent-to-child communication and are reactive by default.
Public properties decorated with @api are reactive. When a parent component changes the value, the child component automatically re-renders.

Basic Usage

Simple Public Property

Expose a property to parent components:
Parent component passes data:
Source: force-app/main/default/lwc/apiProperty/apiProperty.js:3-9

Multiple Public Properties

A component can expose multiple public properties:
Source: force-app/main/default/lwc/child/child.js:3-10

Getters and Setters

Use @api with getters and setters to add custom logic when properties change:
When using getters and setters with @api, place the decorator before the getter, not the setter.
Source: force-app/main/default/lwc/todoList/todoList.js:14-21

Spreading Properties

Pass multiple properties as an object using the spread syntax:
In the template, use the spread operator:
Source: force-app/main/default/lwc/apiSpread/apiSpread.js:3-23

Best Practices

  • Reactive by default: Public properties automatically trigger re-renders when changed
  • Use private fields: Store internal state in private properties (prefixed with _)
  • Immutability: Always create new objects/arrays instead of mutating existing ones
  • Validation: Add validation logic in setters before assigning values
  • Computed properties: Use getters to derive values from public properties