Getting Started with LWC · Lesson 3 of 25
LWC Component Structure: Your First Component
A Lightning Web Component is a folder of files that share the folder's name. Two are required – the HTML template and the JavaScript class – and the configuration file decides where admins can use the component. Knowing exactly what goes in each file, and the naming rules that connect them, saves a lot of confusing deployment errors.
In this lesson
- The files of a component
- Naming rules: camelCase folders, kebab-case tags
- Building a first component
- The js-meta.xml configuration file
The files
force-app/main/default/lwc/courseCard/
- courseCard.html – the template (required for UI components)
- courseCard.js – the JavaScript class (required)
- courseCard.css – styles scoped to this component (optional)
- courseCard.js-meta.xml – configuration: API version, visibility, targets (required to deploy)
- __tests__/courseCard.test.js – Jest tests (optional, never deployed)
- other .js files – helper modules imported by the component (optional)
Naming rules
- The folder and its files use camelCase starting with a lowercase letter:
courseCard. - In markup, a component is used in kebab-case with the namespace prefix
c-:<c-course-card>. Capital letters become a hyphen plus lowercase letter. - Names must be unique in the namespace and can't contain two consecutive underscores or end with an underscore.
- The JavaScript class name is up to you, but by convention it's PascalCase:
CourseCard.
Your first component
<!-- courseCard.html -->
<template>
<lightning-card title={title} icon-name="standard:education">
<div class="slds-p-horizontal_small">
<p>{lessons} lessons · {level}</p>
<p class="tagline">{tagline}</p>
</div>
<lightning-button slot="actions" label="Enroll" variant="brand" onclick={handleEnroll}></lightning-button>
<p slot="footer">{enrolledText}</p>
</lightning-card>
</template>
// courseCard.js
import { LightningElement } from 'lwc';
export default class CourseCard extends LightningElement {
title = 'Lightning Web Components';
lessons = 26;
level = 'Beginner to Pro';
enrolled = 1240;
get tagline() {
return this.lessons > 20 ? 'A complete course' : 'A short course';
}
get enrolledText() {
return `${this.enrolled.toLocaleString('en-US')} learners enrolled`;
}
handleEnroll() {
this.enrolled++;
}
}
/* courseCard.css */
.tagline {
color: var(--slds-g-color-neutral-base-50, #747474);
font-style: italic;
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- courseCard.js-meta.xml -->
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>65.0</apiVersion>
<isExposed>true</isExposed>
<masterLabel>Course Card</masterLabel>
<targets>
<target>lightning__AppPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
▌Lightning Web Components 26 lessons · Beginner to Pro A complete course [Enroll] 1,240 learners enrolled › click “Enroll” ▌Lightning Web Components 26 lessons · Beginner to Pro A complete course [Enroll] 1,241 learners enrolled
- Template: always wrapped in
<template>. Curly braces bind a property or getter –{title},{tagline}– with no expressions inside the braces. - Base components:
lightning-cardandlightning-buttoncome from Salesforce's library of 90+lightning-*components. Theslotattribute places content in the card's actions and footer areas. - Attributes: HTML attributes are kebab-case (
icon-name), and map to camelCase properties (iconName). - CSS: styles only apply inside this component (shadow DOM). The variable is an SLDS styling hook with a fallback value.
The js-meta.xml file
| Element | Meaning |
|---|---|
apiVersion | the API version the component is built against – use your org's latest |
isExposed | true to show it in Lightning App Builder and other tools; false for components only used by other components |
masterLabel, description | the name and help text admins see |
targets | where it may be placed: record, app and home pages, flows, Experience Cloud, utility bar… |
targetConfigs | per-target settings such as admin-editable properties and allowed objects (next lesson) |
Common first-deploy errors
- File names that don't match the folder name exactly (including case).
- Using the component as
<c-courseCard>instead of<c-course-card>. - A
targetslist withoutisExposedset totrue. - Expressions in the template like
{lessons > 20}– move logic into a getter.
Practice
Try it yourself
Create a
profileBadgecomponent that shows a name and role in a card titled "Profile", with a "Follow" button that changes to "Following".Show solution
<!-- profileBadge.html --> <template> <lightning-card title="Profile"> <p>{name} · {role}</p> <lightning-button label={buttonLabel} onclick={toggleFollow}></lightning-button> </lightning-card> </template>// profileBadge.js import { LightningElement } from 'lwc'; export default class ProfileBadge extends LightningElement { name = 'Ravi Kumar'; role = 'Salesforce Developer'; following = false; get buttonLabel() { return this.following ? 'Following' : 'Follow'; } toggleFollow() { this.following = !this.following; } }▌Profile Ravi Kumar · Salesforce Developer [Follow] › click “Follow” ▌Profile Ravi Kumar · Salesforce Developer [Following]
Quick check
How do you use the component in folder orderSummary in markup?
<c-order-summary></c-order-summary>.
Which file makes a component available in Lightning App Builder?
The js-meta.xml, with isExposed true and the right targets.
Can a template contain {count + 1}?
No – templates only reference properties; compute values in a getter.
Summary
- A component folder holds .html, .js, optional .css and the .js-meta.xml configuration.
- Folders are camelCase; markup uses c- plus kebab-case.
- Templates bind properties and getters; js-meta.xml controls exposure and targets.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.