Beginner 25 min read

Project: Build a Hello Salesforce Component

By the end of this lesson, you'll be able to:

  • Create a working LWC from scratch with all required files
  • Understand the minimum file structure needed for a deployable component
  • Add a reactive property and a click handler to a component

Prerequisites: "The Lightning Runtime: Where LWCs Can Be Used"

What We're Building

A simple "Hello Salesforce" component: it displays a greeting, and a button that toggles a welcome message. Small enough to be a genuine first project, substantial enough to touch templating, a JavaScript property, and an event handler — the same fundamental loop every component in this course builds on.

The Four Files of an LWC

All four files below live together in one folder, sharing the same name as the component itself (helloSalesforce):

  • .html — the template
  • .js — the JavaScript class
  • .js-meta.xml — configuration and exposure
  • .css — optional styling

Module 4's "Anatomy of a Component" covers this file set in full depth — this project is your first hands-on look.

Building helloSalesforce.html

<template>
    <lightning-card title="Hello Salesforce" icon-name="custom:custom63">
        <div class="slds-p-horizontal_small">
            <p>Hello, {greeting}!</p>
            <lightning-button
                label="Toggle Welcome Message"
                onclick={handleToggleWelcome}>
            </lightning-button>
            <template if:true={showWelcome}>
                <p class="slds-m-top_small">Welcome to Lightning Web Components!</p>
            </template>
        </div>
    </lightning-card>
</template>

lightning-card and lightning-button are Salesforce's own base components — free, pre-built, SLDS-styled building blocks (covered properly starting Module 4). {greeting} binds to a JavaScript property of the same name. if:true={showWelcome} conditionally renders the inner <template> block only when showWelcome is true.

Building helloSalesforce.js

import { LightningElement } from 'lwc';

export default class HelloSalesforce extends LightningElement {
    greeting = 'Salesforce Developer';
    showWelcome = false;

    handleToggleWelcome() {
        this.showWelcome = !this.showWelcome;
    }
}

Every LWC's JavaScript file exports a single class that extends LightningElement — the base class every Lightning Web Component inherits from. Plain class fields (greeting, showWelcome) automatically become reactive properties the template can read and re-render against when they change.

Building helloSalesforce.js-meta.xml

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>60.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>
    </targets>
</LightningComponentBundle>

isExposed set to true is what allows this component to be added to a page from Lightning App Builder at all — without it, the component deploys successfully but never appears as an option for admins.

Deploying and Viewing Your Component

With Salesforce CLI set up (Module 2 covers this fully), deploying is a single command:

sf project deploy start --source-dir force-app/main/default/lwc/helloSalesforce

Then, in Setup, open any Lightning page in App Builder, and helloSalesforce appears in the Custom Components list, ready to drag onto the page.

Exercise

Looking at the four files that make up helloSalesforce, identify which file is the .html template, which is the .js class, and which is the .js-meta.xml configuration — and change the greeting field's value to your own name.

Show hint

Match each file by its extension to the role described in "The Four Files of an LWC" above.

JAVASCRIPT

Exercise

Challenge: change the button's label from "Toggle Welcome Message" to "Show/Hide Welcome", deploy the updated component, add it to a Lightning page in App Builder, and click the button. In one or two sentences, explain in plain English what happens when it's clicked (you do not need to explain the JavaScript syntax — just describe the behavior you observe).

Show hint

You're describing what you SEE happen, not how the code works internally — that's covered starting next module.

JAVASCRIPT

Project: Build a Hello Salesforce Component Quiz

1. What is the minimum set of files a deployable LWC requires?

2. What does setting isExposed to true in .js-meta.xml actually control?

3. A component deploys with no errors but never appears in Lightning App Builder's component list. What is the most likely cause?

4. CSS is a required file for every Lightning Web Component.

5. In the example component, what causes the template to re-render when the button is clicked?

6. A common first-deploy mistake is a component's folder name not exactly matching its file names.

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

The hands-on capstone of Module 1 — a small but complete component touching templating, a reactive JavaScript property, and an event handler, the same basic display-and-interact pattern that underlies virtually every component in this course.