# What are the advantages of leveraging Salesforce Lightning Base Components over native HTML elements?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, Base Components, SLDS

Read it on the site: https://www.forcechallenges.com/interview-questions/what-are-the-advantages-of-leveraging-salesforce-lightning-base-components-over-native-html-elements

## Answer

**Basics:** You could build a date picker with raw `<input>` and CSS. A base component does it for you: correct SLDS styling, keyboard navigation, screen reader support, validation, locale-aware formats and right-to-left support, and it updates when Salesforce updates the design system.

**How it works:**
1. Use the component tag in the template, for example `<lightning-input type="date" label="Start" value={start} onchange={handleChange}>`.
2. Configure with attributes, read state through events (`event.detail`, `event.target.value`).
3. Call public methods like `reportValidity()` on inputs or `lightning-datatable` APIs.
4. Style variations through documented attributes (`variant`, `size`) and styling hooks rather than overriding internals.

**Advanced / deep dive:** Benefits: accessibility (ARIA roles, focus management), internationalization (number, currency, date formats follow user locale and time zone), validation framework (`required`, `pattern`, `setCustomValidity`), performance tuning (for example datatable column resizing, infinite loading, inline edit), and forward compatibility with design updates such as SLDS 2 and the Cosmos theme. Trade-offs: limited customization of internals because of shadow encapsulation; some missing features (no row spanning in datatable, limited layout control); they work inside the Salesforce Lightning runtime, not in arbitrary web pages or plain LWC Open Source. Native HTML is fine for simple structure (`div`, `p`, `ul`), and SLDS blueprints (CSS only) are an alternative when you need markup control, at the cost of reimplementing accessibility yourself. Jest tests need the stubs provided by `@salesforce/sfdx-lwc-jest`.

**Code / config example:**
```html
<lightning-card title="New Task" icon-name="standard:task">
    <div class="slds-p-around_medium">
        <lightning-input type="text" label="Subject" required onchange={handleSubject}></lightning-input>
        <lightning-input type="date" label="Due" onchange={handleDue}></lightning-input>
        <lightning-button variant="brand" label="Save" onclick={handleSave}></lightning-button>
    </div>
</lightning-card>
```

**Hidden gotchas & must-remember points:**
- They are not available outside the Lightning runtime (for example plain web pages or LWC Open Source).
- You cannot restyle internals with parent CSS; use `variant`, `class` on the host, or styling hooks.
- `lightning-input` values come as strings; convert numbers and booleans (`checked` for checkbox).
- Call `reportValidity()` explicitly before saving; displaying errors is not automatic on submit.
- Some components behave differently on mobile or in Experience Cloud; check docs and the Component Library.
- Datatable data must have unique `key-field` values.

**Best practice:** Use base components by default and drop to SLDS markup or native HTML only for gaps they cannot fill.

---
Source: ForceChallenges · last updated 2026-11-02
