Development questions
How do you use a third-party JavaScript library or static resource in LWC?
DevelopmentMedium
- Upload the library as a Static Resource (e.g.
chartjs). - Import the resource and loaders:
javascript
import CHARTJS from '@salesforce/resourceUrl/chartjs';
import { loadScript, loadStyle } from 'lightning/platformResourceLoader';
renderedCallback() {
if (this.loaded) return;
this.loaded = true;
loadScript(this, CHARTJS)
.then(() => this.initChart())
.catch(e => console.error(e));
}
loadScript/loadStyle return promises; load once, guard with a flag, and the library must be compatible with Lightning Web Security / Locker. Never load scripts from external CDNs (CSP blocks them unless allow-listed in CSP Trusted Sites).
lwcstatic-resourcesthird-party
More Development questions
- What is an Apex Trigger in Salesforce and when should you use it over Flows?
- What is the difference between before triggers and after triggers?
- Explain all Trigger context variables (isInsert, isUpdate, isDelete, isUndelete, isBefore, isAfter, isExecuting, size, operationType).
- What is the difference between Trigger.new and Trigger.old?
- In which trigger events are Trigger.new and Trigger.newMap available?
- In which trigger events are Trigger.old and Trigger.oldMap available?