Your First Steps with LWC Recipes
This guide will help you explore the Lightning Web Components Recipes app and understand how to use the examples to build your own components.Before starting, make sure you’ve completed the Installation and have the LWC Recipes app running in your Salesforce org.
Exploring the App
Launch LWC Recipes
- Open your Salesforce org
- Click the App Launcher (waffle icon)
- Select LWC or LWC Recipes from the app list
Browse recipe categories
The recipes are organized into logical categories:
- Hello - Basic concepts and property binding
- Composition - Component composition and iteration
- Event Handling - Component events and communication
- Data Access - Apex integration and data management
- Lightning Data Service - Working with LDS and UI API
- Navigation - Navigate to records, pages, and lists
- Third-Party Libraries - Integrate external JavaScript libraries
Understanding the Code Structure
Each Lightning Web Component consists of at least three files:Recipe #1: Hello World
Let’s start with the simplest example - property binding.What It Does
Thehello component demonstrates how to bind a JavaScript property to the HTML template using curly braces {}.
The Code
Key Concepts
- Property Declaration:
greeting = 'World'defines a reactive property - Data Binding:
{greeting}displays the property value in the template - Lightning Card: Uses the
<lightning-card>base component for consistent styling
Recipe #2: Calling Apex Methods
One of the most common tasks is retrieving data from Salesforce using Apex.Imperative Apex Call
TheapexImperativeMethod component shows how to call Apex methods imperatively using async/await.
Key Concepts
- @salesforce/apex Import: Import Apex methods using the wire format
- Async/Await: Modern JavaScript pattern for handling asynchronous operations
- Error Handling: Always use try/catch to handle potential errors
- @AuraEnabled: Required annotation for Apex methods called from LWC
Recipe #3: Using Wire Adapters
Wire adapters provide reactive data binding to Salesforce data and metadata.Wire Adapter for UI API
ThewireGetRecord component uses the @wire decorator to fetch record data.
Key Concepts
- @wire Decorator: Automatically invokes the wire adapter when parameters change
- Reactive Parameters: Use
$prefix (e.g.,$userId) for reactive properties - Schema Import: Import field references using
@salesforce/schema - Current User: Access logged-in user ID with
@salesforce/user/Id
Recipe #4: Component Events
Components communicate through custom events.Simple Event Handling
TheeventSimple component demonstrates basic event handling.
Key Concepts
- Event Handlers: Use
onclick={handlerName}to bind event handlers - Reactive Properties: Any property used in the template is reactive
- Conditional Attributes: Dynamically disable buttons based on state
Recipe #5: Rendering Lists
Display collections of data usingtemplate for:each.
Iteration Example
ThecompositionIteration component shows how to render a list of contacts.
Key Concepts
- for:each Directive: Loop through arrays in templates
- for:item: Defines the variable name for each iteration
- key Attribute: Required for performance - use unique identifiers
- Component Composition: Render child components in loops
Recipe #6: Lightning Data Service
Create records using Lightning Data Service UI API.Creating Records
TheldsCreateRecord component demonstrates the createRecord API.
Key Concepts
- createRecord API: Create records without Apex
- Schema Imports: Reference objects and fields safely
- Toast Events: Display success/error messages to users
- Error Handling: Use utility functions to format error messages
Recipe #7: Using Third-Party Libraries
Integrate JavaScript libraries like Chart.js for data visualization.Chart.js Integration
ThelibsChartjs component shows how to load and use external libraries.
Key Concepts
- Static Resources: Upload libraries as static resources
- loadScript API: Load external JavaScript files
- renderedCallback: Lifecycle hook that runs after rendering
- Initialization Flag: Prevent multiple library loads
Recipe #8: Navigation
Navigate to record pages programmatically.NavigationMixin Example
ThenavToRecord component uses the Navigation service.
Key Concepts
- NavigationMixin: Extend your component with navigation capabilities
- Page Reference: Define navigation targets with type and attributes
- Standard Pages: Navigate to view, edit, or create record pages
- Action Names: Use ‘view’, ‘edit’, or ‘new’ for different actions
Build Your First Component
Now that you’ve explored the recipes, try building your own component:Create a new component
In VS Code with Salesforce extensions:
- Press Cmd+Shift+P (Mac) or Ctrl+Shift+P (Windows)
- Select SFDX: Create Lightning Web Component
- Name your component (e.g.,
myFirstComponent)
Add component logic
Choose a recipe that matches your use case and adapt the code:
- Need to fetch data? Start with
apexImperativeMethodorwireGetRecord - Building a form? Look at
ldsCreateRecord - Displaying a list? Check out
compositionIteration - Need events? Explore
eventSimpleoreventWithData
Best Practices from the Recipes
Use Wire Adapters
Prefer
@wire for data binding - it’s reactive and efficientHandle Errors
Always use try/catch blocks and display user-friendly error messages
Import Schemas
Use
@salesforce/schema imports for type-safe field referencesFollow Naming Conventions
Use camelCase for properties and methods, PascalCase for component names
Learning Path
Week 1: Basics
- Explore
hello,clock,eventSimple - Learn property binding and event handling
- Build a simple counter or calculator component
Week 2: Data Access
- Study
apexImperativeMethod,wireGetRecord - Understand wire adapters vs imperative calls
- Create a component that displays Salesforce data
Week 3: Advanced Features
- Dive into
ldsCreateRecord,compositionIteration - Learn Lightning Data Service and component composition
- Build a multi-component application
Additional Resources
Complete Trailhead
Earn a badge while learning the recipes
LWC Dev Guide
Official Lightning Web Components documentation
Component Reference
Browse all base Lightning components
GitHub Issues
Ask questions and report issues
Pro Tip: Use the Code Tours feature in VS Code to get guided walkthroughs of complex recipes. Install the CodeTour extension to get started.
