0

I'm trying to retrieve data from Apex using @wire. However, I do not want to fire this event before renderedCallback(). This is because I'm receiving a parameter required to fire a wired event which is initially set as undefined. This parameter is not used in HTML.

@api myRecords = [];

@track _myParam;
@api
get myParam() {
    return this._myParam;
}
set myParam(val) {
    this._myParam = val;
}

renderedCallback(){
    console.log("---myParam: " + this.myParam);
}

@wire(getInformation, {param1: this.myParam})
wiredAttachments({ error, data }){
    if (data){
        this.myRecords = data;
    } else if (error){
        //do something
    }
}

I looked at a similar question but unable to figure it out. Any help is appreciated.

1 Answer 1

5

A strong side of functions decorated with @wire is that it is possible to use reactive variables, which are prefixed with $ in the adapter config object. Whenever the reactive variable changes - the wire method will be executed. In order to use a reactive variable as a param, omit this and use the $ symbol as a string.

@api myRecords = [];

_myParam;

@api
get myParam() {
    return this._myParam;
}
set myParam(val) {
    this._myParam = val;
}

@wire(getInformation, {param1: '$_myParam'})
wiredAttachments({ error, data }){
    if (data){
        this.myRecords = data;
    } else if (error){
        //do something
    }
}

refer Understand the Wire Service documentation to learn more about it

You must log in to answer this question.

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