LWC Component with Different Context based on Record Page v Home Page

lightning-web-components

I have a component that is just filling out a lightning-datatable. When opened on a record page, it fills with data specific to that record. This is working as expected. But I would also like to have it fill with all currently active data created by the current user (not record-specific) if it is opened on a home page.

Here is what I am using for when the recordId exists:

    @wire(getList,{accId:'$recordId'})
    list(result){
        morestuff;
    }

But when the recordId is undefined, the getList method never runs. Is there a way to easily go about this? Basically the getList function just has a different SOQL query returned based on whether the recordId exists or not. But the function never runs if it doesn't exist (understandable, per the important note in the documentation)

Best Answer

You can use a getter/setter to insert a null value:

_recordId;
@api set recordId(value) {
  this._recordId = value || null;
}
get recordId() {
  return this._recordId;
}

_recordId is just a notation I use for backing/internal variables, you can name it whatever you like. You might need to adjust your wire method to for reactivity to work, as the documentation isn't clear on that.