contact@itechcloudsolution.com +91 997 9933 595 +91 972 6015 295
209-210-211, Western Plaza, Simada Naka, Surat, Gujarat, India 395006.
iTechCloud Solution
Book a call→
Development

How to Create Custom Components Using LWC

How to Create Custom Components Using LWC

Subhash Panchani headshot
Subhash Panchani
Founder & CEO · iTechCloud Solution
·4 min read
How to Create Custom Components Using LWC cover image

In the world of Salesforce development, Lightning Web Components (LWC) have become one of the most powerful ways to build modern, fast, and efficient user interfaces. LWC is built on the latest web standards, making it lightweight and high-performing. If you’re a Salesforce developer looking to build custom components that are reusable, responsive, and scalable, understanding LWC is essential.

This comprehensive guide will walk you through how to create custom components using LWC, from setup to deployment, along with best practices and tips.

Table of Contents

What Are Lightning Web Components (LWC)?Step 1: Create a Salesforce DX ProjectStep 2: Create a Lightning Web ComponentStep 3: Understanding the Component StructureStep 4: Deploy Component to Salesforce OrgStep 5: Use Your Component in Lightning App BuilderStep 6: Adding CSS Styling to Your ComponentStep 7: Communicating Between ComponentsStep 8: Best Practices for LWC DevelopmentStep 9: Debugging and TestingConclusion

What Are Lightning Web Components (LWC)?

Lightning Web Components (LWC) is a modern framework developed by Salesforce that allows developers to build custom, reusable components. It is based on native web standards like Web Components, Custom Elements, Shadow DOM, and ES Modules, which makes the development process faster and more aligned with modern JavaScript.

Why Choose LWC?

1. Performance:

Faster and more lightweight compared to older frameworks like Aura.

2. Standards-Based:

Uses modern JavaScript, HTML, and CSS.

3. Secure:

Built-in security with Salesforce Locker Service.

4. Reusable:

Components can be reused across different Salesforce pages and apps.

5. Seamless Integration:

Direct access to Salesforce data via Apex and Lightning Data Service.

Step 1: Create a Salesforce DX Project

First, you need to create a Salesforce DX (SFDX) project to manage your component development.

Command to create a project:

sfdx force:project:create –projectname myLwcProject

Navigate to your project directory:

cd myLwcProject

Authorise the Salesforce Org to connect to your project:

sfdx force:auth:web:login -a DevHub

Step 2: Create a Lightning Web Component

Once your project is set up, create a new component using the following command:

sfdx force:lightning:component:create –type lwc –componentname helloWorld –outputdir force-app/main/default/lwc

This will generate a folder named ‘key_files’ helloWorld containing three key files:

helloWorld.htmlThe template for UI.

helloWorld.jsThe JavaScript controller for logic.

helloWorld.js-meta.xmlThe metadata file that controls visibility and configuration.

Step 3: Understanding the Component Structure

1. HTML File (User Interface)

This file defines the visual structure of your component using standard HTML along with LWC-specific syntax.

Example:

Hello, {name}!

2. JavaScript File (Logic Layer)

Handles the component’s data and logic.

import { LightningElement } from ‘lwc’;

export default class HelloWorld extends LightningElement {
name = ‘World’;

handleChange(event) { this.name = event.target.value; }}

3. Meta XML File (Configuration Layer)

Controls where the component can be used (App Builder, Record Pages, etc.).

58.0

true

lightning__AppPage

lightning__RecordPage

lightning__HomePage


Step 4: Deploy Component to Salesforce Org

To make your component available in Salesforce:

Deploy source:

sfdx force:source:push

Now, you can use this component in Lightning App Builder and Record Pages.

Step 5: Use Your Component in Lightning App Builder

Go to Salesforce Setup.

Navigate to Lightning App Builder.

Create or edit a page (App/Home/Record Page).

Find your hello world component in the custom components list.

Drag and drop it onto the page.

Save and activate the page.

Congratulations! Your first custom LWC component is live.

Step 6: Adding CSS Styling to Your Component

To add custom styling, create a CSS file named helloWorld.css inside the component folder.

Example:

h1 {

color: blue;

font-size: 24px;

}

Salesforce also allows scoped styles using :host a selector.

Example:

:host {

display: black;

border: 1px solid #ddd;

padding: 10px;

border-radius: 8px;

}

Step 7: Communicating Between Components

Sometimes, you may need to share data between components. You can achieve this using public properties, custom events, and Apex methods.

Using Public Properties:

export default class ChildComponent extends LightningElement {

@api message; // Public property

}

Using Custom Events:

this. dispatchEvent(new CustomEvent(‘myevent’, { detail: ‘Data from child’ }));

Handling Events in Parent:

Apex Method (server-side call):

import getAccounts from ‘@salesforce/apex/AccountController.getAccounts’;

connectedCallback() {
getAccounts(). then(result => {
this. accounts = result;
}). catch(error => {
console.error(error);
});
}

Step 8: Best Practices for LWC Development

1. Follow Naming Conventions: Use kebab-case for component names.

2. Use decorators properly,@wire as needed.

3. Avoid DOM Manipulation: LWC handles DOM updates, so avoid manual changes.

4. Modular Components: Keep components small, reusable, and focused on a single responsibility.

5. Handle Errors Gracefully: Always handle exceptions, especially with Apex calls.

6. Security First: Never expose sensitive data. Use Salesforce Locker Service guidelines.

Step 9: Debugging and Testing

1. Console Logs: Use console.log() to debug in browser dev tools.

2. LWC Jest Tests: Write unit tests using the Salesforce-supported Jest framework.

3. Salesforce Lightning Testing Service (LTS): For end-to-end testing.

4. Browser DevTools: Inspect components using Chrome DevTools.

Building custom components using Lightning Web Components is a crucial skill for Salesforce developers aiming to create modern, responsive, and reusable UI elements. By leveraging JavaScript, HTML, and CSS, developers can build powerful custom solutions directly integrated with Salesforce data and workflows.

Subhash Panchani headshot
Written by
Subhash Panchani
Founder & CEO · iTechCloud Solution

Founder and CEO of iTechCloud Solution, a Salesforce consulting and product partner since 2017. He sets engagement strategy and stays the commercial owner of every account, working with clients across Salesforce, AI and data from the first call through to go-live.

Connect on LinkedIn
Put it to work

Want this applied to your org?

Book a free discovery call and we'll map the highest-ROI next step for your team.

Book a call →

Related insights

All articles →
Salesforce Agentforce Observability: A Practical Guide
Agentforce & AI
Oct 30, 2026 · 9 min read

Salesforce Agentforce Observability: A Practical Guide

Read article →
Salesforce Agentforce Coworker: Features and Business Uses
Agentforce & AI
Oct 29, 2026 · 9 min read

Salesforce Agentforce Coworker: Features and Business Uses

Read article →
How Salesforce Agentforce Can Improve Lead Conversion
Agentforce & AI
Oct 27, 2026 · 7 min read

How Salesforce Agentforce Can Improve Lead Conversion

Read article →

Have a similar problem?

Send us the shape of it. We will tell you honestly whether we are the right team.

  • Response within one business day
  • Salesforce-certified architects on the call
  • Delivery across US, UK, EU, Middle East and APAC