4

I would like to prevent the @wire function from loading automatically on initialization and instead load it on command.

Currently I am using refreshApex() now to load it on command, but I don't want to load it until a button triggers it to load.

The purpose of my question is to add a Sync button which load a good amount of data on click only.

How can I achieve this?

2 Answers 2

4

Don't call it via the wire service. Call the Apex method imperatively in your button's onclick handler.

3
  • 2
    Suggesting an alternative like this is a good thing to do. Of course, depending on the wire (e.g. if it is a built-in Salesforce wire service) you may not be able to call it imperatively.
    – Phil W
    Commented Nov 28, 2023 at 16:17
  • Valid point, @PhilW!
    – Wes Lyons
    Commented Nov 28, 2023 at 16:54
  • 1
    Thank you, this is exactly what I needed! For my case I also added cachable = false to the apex method
    – Z0q
    Commented Nov 29, 2023 at 10:37
5

Wire methods with reactive properties don't call Apex until their reactive values are set to any value other than undefined. In other words, if you do this:

someProperty;
@wire(someMethod, { someProperty: '$someProperty' }) someWireProperty;
handleClick() {
  this.someProperty = someNonUndefinedValue;
}

It won't call the Apex method until the parameters have definition.

Note that if you have a handler instead of a property, it'll still call the method once to set the default object ({ data: undefined, error: undefined }).

3
  • Wire doesn't get called until referenced properties are undefined. It will get called as soon as they have some values other than undefined including null. I think your statement is other way around? Commented Nov 28, 2023 at 16:39
  • 1
    @RahulGawale I see the wording is confusing. I did say "non-undefined" (as in, not undefined). I'll clarify.
    – sfdcfox
    Commented Nov 28, 2023 at 16:42
  • Does it start automatically perhaps because I am using an @api defined variable? I don't even know why I am using this in the first place
    – Z0q
    Commented Nov 29, 2023 at 8:42

You must log in to answer this question.

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