If you are new to Salesforce frontend development, building your very first Lightning Web Component is an exciting milestone. LWC gives developers the power to construct responsive, high-performance web applications that integrate seamlessly with Salesforce data.
In this guide, we will walk through the exact steps required to create a simple "Hello World" component complete with HTML markup, custom styling, and reactive JavaScript logic.
Prerequisites
- A Salesforce Developer Edition account or sandbox.
- Basic familiarity with HTML, CSS, and JavaScript.
Step 1: Create a New Lightning Web Component
To begin, log in to your Salesforce account and open the Developer Console. From the top menu, select File > New > Lightning Web Component, name your component helloWorld, and submit.
Step 2: Write the HTML Markup
Open your new helloWorld.html file and replace the default template code with the following structure:
<template>
<div class="container">
<h1>Hello, Lightning Web Components!</h1>
<lightning-button label="Click Me" onclick={handleClick}></lightning-button>
<p>{greeting}</p>
</div>
</template>
Step 3: Add CSS Styling
LWC automatically scopes CSS stylesheets to individual components, meaning your styles won't accidentally leak out and break other parts of Salesforce. Create a stylesheet named helloWorld.css and add the following design rules:
.container {
text-align: center;
padding: 20px;
background-color: #f0f0f0;
border-radius: 8px;
}
h1 {
font-size: 24px;
margin-bottom: 20px;
color: #1f4e79;
}
p {
font-size: 18px;
font-weight: bold;
color: #262626;
margin-top: 15px;
}
Step 4: Implement the JavaScript Controller
Open your helloWorld.js file. This controller handles user events and manages the component's reactive state.
import { LightningElement, track } from 'lwc';
export default class HelloWorld extends LightningElement {
// Reactive property to store the greeting message
@track greeting = '';
handleClick() {
this.greeting = 'Hello, you clicked the button!';
}
}
While older versions of LWC required the
@track decorator for almost all reactive variables, modern LWC automatically tracks primitive variables (like strings, booleans, and numbers) declared in your class. You only need @track today if you are tracking deep mutations inside objects or arrays.
- The user clicks the
<lightning-button>. - The button's
onclick={handleClick}attribute triggers the JavaScript function. - The function updates
this.greetingwith a new string. - Because
greetingis bound in the HTML template via{greeting}, the browser re-renders the paragraph tag instantly.
- HTML (
.html): The user interface template. - JavaScript (
.js): The controller logic and event handlers. - CSS (
.css): Component-scoped styling. - Configuration (
.js-meta.xml): Defines where and how the component can be exposed in Salesforce Lightning Experience.
Conclusion
Congratulations! You have successfully built, styled, and tested your very first Lightning Web Component. While this example is simple, it covers the foundational concepts—event handling, data binding, and component structure—that you will use when building complex enterprise applications on the Salesforce platform.
Happy coding!