Best Practices for Salesforce Lightning Web Components (LWC)
Always Name Carefully
- Always name variables, methods, and components carefully.
- Do not start a property name with ‘on’, ‘aria’, or ‘data’.
- Do not use reserved keywords like ‘slot’, ‘part’, or ‘is’.
Naming Convention
- Try to name your files in the below format
- Component Name – camelCase
- Component Class Name – PascalCase
- Component reference – kebab-case
- HTML Attribute Name – kebab-case
Don't forget to check out: Assign Permission Set at the User Creation Made Simple: What You Need to Know | Salesforce Guide
Use Spread Operator for adding additional attributes in data returned from Apex
- We frequently need to add extra properties to the data returned from apex according to the requirement.
Be mindful of Case Sensitivity
- Lightning Web Components are case-sensitive.
- Be very careful while writing your writing code.
Instead of Expressions, Use Getters
- Use a JavaScript getter, to compute a value for a property.
- Getters are more powerful than expressions because these are JavaScript functions and also enable unit testing, which reduces bugs and increases the fun.
Do not forget to assign a key to each
- While using iteration in LWC, do not forget to assign the key attribute within the scope of the iteration.
- Otherwise, you will get the below error: " Elements within iterators must have a unique, computed key value. "
Use an iterator to apply special rendering to the first and last elements of list
- If you want to apply a special style or rendering to the first and last element of the list, use iterator instead of for:each.
- Iterator has two more attributes: first and last.
Pass only primitive data in custom event
- As one of the best practices, pass only primitive data types in custom events.
- JavaScript passes all data types by reference except for primitive data types.
- So, any listener can mutate the actual object which is not good practice.
- If at all we need to pass an object, we need to copy the data to a new object before sending it so that it does not get changed by any of the listeners.
Use LDS whenever possible
- Instead of making apex calls to get the data, try to use LDS.
- It requires no apex code and can perform Create, Read, Update, Delete operations on a single record.
- It also takes care of the sharing rules and field-level security.
Avoid using hardcoding, use custom labels
- Similar to apex, avoid using hardcoding in LWC.
- Keep the text inside a custom label and configure it as and when required without making any changes to existing code.
Don't forget to check out: Assign Permission Set at the User Creation Made Simple: What You Need to Know | Salesforce Guide
Include error handling in the code logic
- We should consider the error scenario as well and notify the user with a user-friendly error message.
- For this, we can make use of toast messages to show high-level detail to users on what went wrong.
Specify fields instead of layout in lightning-record-form
- To improve performance, specify specific fields instead of layout in lightning-record-form.
- If we specify the layout, the component must handle receiving every field that is assigned to the layout for the context user.
Responses