Lwc: querySelector result is always empty if file makes a non-default export

Created on 26 May 2020  路  5Comments  路  Source: salesforce/lwc

Description

If an LWC makes a default export for its component, plus an additional export, then template.querySelectorAll('div') (or any selection) is empty. Removing the additional export removes this bug.

Steps to Reproduce

Using this template:

<template>
    <div>what in the world</div>
</template>

And this JavaScript:

import { LightningElement, api } from "lwc";

// THIS IS THE MONEY LINE
export const t1 = 123;

export default class MyComponent extends LightningElement {
    @api reportValidity() {
        console.log('div count', this.template.querySelectorAll('div').length);
    }
    renderedCallback() {
        console.log('div count', this.template.querySelectorAll('div').length);
    }
}

The query selector will always return 0 results, no matter what.

If you remove the export const t1 line (so there is only the default export) this problem disappears.

You can see the diff between the compiled versions here: https://www.diffchecker.com/iFhvJQBF

I tried to repro this in the playground but couldn't: https://developer.salesforce.com/docs/component-library/tools/playground/ol0g50Fk0/1/edit

However I don't think it's any issue in our org, as it's a pretty simple dev org.

Expected Results

The query selector is not empty.

Actual Results

The query selector is empty.

Browsers Affected

Chrome 81 and probably more.

Version

Not sure, this is in Salesforce, API version 48.

BUG P3 Lightning Locker

All 5 comments

Error while creating work item!

After debugging this looks like a locker issue.
/cc @caridy

Error while creating work item!

Afternoon! Just writing to say that this is still an issue in API version 50. FYI, no work item has been created for this.

This issue has been linked to a new work item: W-8386672

Was this page helpful?
0 / 5 - 0 ratings