javascript - Redeclaration of Let error - How to find where it's being reassigned?
Solution:
You are declaringcollectionSelector
variable two times Infor loop
for (let collectionSelector of collectionSelectors) {// fist time
let blockId = collectionSelector.dataset.blockId;
let collectionCarousel = document.querySelector(`${sectionSelector} .recommendations__collection[data-block-id="${blockId}"]`);
let otherCarousels = document.querySelectorAll(`${sectionSelector} .recommendations__collection:not([data-block-id="${blockId}"])`);
collectionSelector.addEventListener('click', () => {
for (let otherCarousel of otherCarousels) {
otherCarousel.classList.remove('active');
}
for (let collectionSelector of collectionSelectors) { // 2nd time
collectionSelector.classList.remove('active');
}
collectionSelector.classList.add('active');
collectionCarousel.classList.add('active');
window.dispatchEvent(new Event('resize'));
})
}
I the firstfor loop
scope there is already a variable calledcollectionSelector
, that's why there is an error. Change that to some another variable name, error should be fixed.
Answer
Solution:
An iteration variablecollectionSelector
is declared in the first loop, and then in the loop inside the event listener you declare the iterator the same way, bbut the second loop shares the scope of the first one, just change the name in the second loop
for (const item of collectionSelectors) {
item.classList.remove('active');
}
Source
Didn't find the answer?
Our community is visited by hundreds of Shopify development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.