Although, above answers are correct, there is one more problem regarding showing new slides with updated/new data after array has been updated and then need to refresh the slick slider instance.
If we don't do like this, then somehow slick keeps showing older data which was bounded there before updating the array even if the array is already updated.
So the code with newer frameworks available and ES6 approach might look something like this.
Let's say we are showing array of data via loop in modern library like vue, react, angular or alpine js and we have array of shops
to show as a slider.
Each time someone triggers some action, array(and eventually dom) is updated but we have to manually tell slick slider to update the slides data as per the data in the new array. here we have data in the form of shops
array.
{
shops: [],
init() {
let self = this;
// watch for your data and provide callback function after it has been updated
// this $watch is example from alpine js but similar could be available in other framrworks
self.$watch('shops',(currentShops,oldShops) => {
//currentshops holds the new value of shops after update
//oldShops holds older value before update
//first remove all slides that were attached with slick before shops array was updated (sorted for example)
$('#shops-section-slider.slick-initialized').slick('slickRemove', null, null, true);
//After this, add new slides after shops has been updated(sorted)
// via template that accepts shop as an argument
currentShops.forEach(function(shop,index) {
$('#shops-section-slider.slick-initialized').slick('slickAdd', self.getSliderTmpl(shop));
})
//after adding and removing is completed,
//finally just call refresh to prevent reinit entirely.
$('#shops-section-slider.slick-initialized').slick('refresh');
})
//shopsPromise only does slider creation and other things when data has been updated in dom.
shopsPromise.then(function(data) {
//update shops here via data that has been recieved
shops.push(data)
}).then(function() {
//crate slider after promise is resolved
$('#shops-section-slider').not('.slick-initialized').slick({
//your slick settings
})
});
},
getSliderTmpl(shop) {
return `<div class="col-12 col-md-6 col-lg-4 col-xl-4 d-flex flex-column shops-section">
<div class="row flex-grow-1 flex-column flex-xxl-row">
<div class="col-12 col-xxl-6">
<div class="shops-img position-relative">
<strong class="shops-title-card d-block d-xxl-none">${shop.whateveravailable}</strong>
</div>
</div>
</div>
</div>`;
}
}
and inside the html, we even don't need the <template>
element which is used as looping in alpine js as it creates incorrect width and issues due to one more slides there so we can omit that, and thus managing entire slider data and template both from js only.
<div class="row shops-section-wrapper" id="shops-section-slider"></div>