Wire Adapter not handling $recordId as reactive in LWC in Community

lightning-communitylightning-web-componentslwc-wire-adapter

I have an LWC on a community which is working perfectly outside of the community but when ran on the community to pull the recordId from the url, it tries the wire with the initial blank value, then after it pulls in the recordId it isn't running the wire reactive again. It seems to work on my sandbox as it can find the record quick enough to not fail the first time, but in production it seems to take a little too long to grab the recordId from the URL. I also tried doing it manually with getters and setters but to no avail.

I have done all field and object importing above the copied code but as explained above, all is working outside of the community so I do not believe it to be at fault.

JS

const fields = [FIELD1,FIELD2];

export default class userLWC extends NavigationMixin(LightningElement) {

    @api recordId;
    @track isSpinner = false;

    connectedCallback() {
        this.isSpinner = true;
    }

    @wire(getRecord, { recordId: '$recordId', fields })
    handleWiredRecord(result){
        this.record = result;
        console.log('result==>'+result);
        const {error,data} = result;
        if (error){
            this.dispatchEvent(new ShowToastEvent({
                title: 'Error loading Record',
                message: error.body.message,
                variant: 'error'
            }),);
            console.warn(error);
        } else if (data){
            this.isSpinner = false;
            console.log('data==>'+data);
            this.record = data;
        }
    }

HTML

<template>
    <template if:true={isSpinner}>
        <div class="spinner">
            <lightning-spinner alternative-text="Loading" size="medium"></lightning-spinner>
        </div>
    </template>
    <lightning-card title="Test">
        <template if:false={isSpinner}>
            <div class="slds-var-m-around_medium">
                <div class="slds-float_right">

Meta

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>52.0</apiVersion>
    <isExposed>true</isExposed>
    <masterLabel>Test Component</masterLabel>
    <description>Test Component.</description>
    <targets>
        <target>lightning__RecordPage</target>
        <target>lightningCommunity__Page</target>
        <target>lightningCommunity__Default</target>
    </targets>
    <targetConfigs>
        <targetConfig targets="lightningCommunity__Default">
            <property name="recordId" type="String" label="Record ID" description="Should be set to {!recordId}" />
        </targetConfig>
    </targetConfigs>
</LightningComponentBundle>

Best Answer

As far as possible, try to avoid calling AuraEnabled methods via wire . Sometimes wire works perfectly sometimes it doesn't . Even if wire is an important concept and a good way of calling AuraEnabled methods , do not use wire when you experience problems while calling . So try calling your AuraEnabled methods imperatively using the same reactive variable. If reactive variable doesn't work add your imperative calling inside a method and call it when needed

Please let me know if it worked or not

Related Topic