1

I am using refreshApex to get the latest data with a button. There is a possibility that there are no new changes and I want to display a message indicating there are no new changes.

Question

  1. How can I detect if there are changes or no changes when using the refreshApex method?

What I tried so far but no luck

myComponent.js

import { LightningElement, api, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';

export default class Record extends LightningElement {
    @api recorded;
    wiredRecord;

    @wire(getRecord, { recordId: '$recordId', fields: ['Account.Name'] })
    wiredActivity(value) {
        this.wiredRecord = value;
        const {data, error} = value;
        if(data) { ... }
        else if (error) { ... }
    }

    handleClick() {
       refreshApex(this.wiredRecord);
       // I can't figure out how to check if different ---- help here
    }
}

myComponent.html

<template>
...
   <button onclick={handleClick}>My button</button>
...
</template>

1 Answer 1

0

refreshApex is asynchronous, so you can copy the old value and compare to the new after waiting for new results:

async handleClick() {
  let oldData = JSON.stringify(this.wiredRecord.data);
  await refreshApex(this.wiredRecord);
  let newData = JSON.stringify(this.wiredRecord.data);
  if(oldData === newData) {
    // Nothing has changed //
  }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .