In mode we can change to Edit, view directly, then why do we need
lightning-record-form renders the form based on the standard layouts configured. Here are the reasons we use it.
- Switches between view and edit modes automatically when the user
begins editing a field in a view form
- Provides Cancel and Save buttons automatically in edit forms
- Uses the object's default record layout with support for multiple
- Loads all fields in the object's compact or full layout, or only the
fields you specify
So basically, we can not controlled which fields are displayed. The fields displayed based on the layout type and layout defined.
The reason we use
lightning-record-edit-form instead of form is that you can explicitely render the fields, let's say you want dynamically show/hide the field based on anothe field value. Also,
- Editing a record's specified fields, given the record ID.
- Creating a record using specified fields.
- Customizing the form layout
- Custom rendering of record data
Q2 & Q3
lightning-record-view-form also we will able to create records then Why do we need UI API for creating Records
When we have to go for UI API
We use UI Record API when we need to programmatic access to the record but the UI form is not needed. Like the record input is hidden from user.
Example: Use geRecord to get the record which you don't want to show on page, but it is needed for some calculation or to set components' visibility.
How to select between these options?
Thumb rule is to go for easy solution which is record-form, then check if is suitable for your requirement, if not then you can choose other options.
Specifically you can decide by answering these questions?
- Do we need to utilize the sandard layout or customized layout? So whatever fields on the layout will be automcatically available on the form.
- Do we need a custom styling to display a record?
- Do we need to control the visibility of the fields based on the field values on record?
- Do we need to show the record to the user?