Being able to synchronize data without reloading the entire page is a key user experience requirement.
In the Spring '23 release, the new lightning/refresh
module and the RefreshView
API provide a standard way to refresh component data in both LWC and Aura.
<aside> ☝ Lightning Web Security must be enabled in orgs where components use the RefreshView API. Lightning Locker doesn’t support RefreshView API.
</aside>
To use RefreshView API, import the lightning/refresh
module.
Let’s imagine that you have developed a custom component to create a new contact, and in the same Tab, you have a standard LWC to show a list of all the contacts.
When you create a new contact with your custom LWC, unfortunately the standard LWC List View will not show the new contact, you must refresh the whole page using your browser reload button.
RefreshEvent
event, defined in lightning/refresh
.So, when a LWC calls to this.dispatchEvent(new RefreshEvent())
function, all standard components that are aware (either because their already has this standard behavior or because they registered using the technical term) of this event, will refresh automatically.
import { LightningElement } from 'lwc';
import { RefreshEvent } from 'lightning/refresh';
export default class createCustomContact extends LightningElement {
createNewAccount() {
...
**this.dispatchEvent(new RefreshEvent());
....**
}
}
RefreshEvent
Now, let’s imagine that your custom LWC is the one that needs to be aware of a refresh signal sent by other LWC components.
To be notified of a signal refresh event, a component registers a handler method: