Web / TanStack Interview Questions
What is TanStack Virtual and when should you use it?
TanStack Virtual renders only the DOM nodes visible in the viewport. With thousands of rows, rendering everything creates thousands of DOM nodes causing slow renders, janky scroll, and high memory use. TanStack Virtual computes which items are in view and renders only those plus a small overscan buffer.
<script setup lang="ts">
import { useVirtualizer } from "@tanstack/vue-virtual"
import { ref, computed } from "vue"
const parentRef=ref<HTMLDivElement|null>(null)
const items=Array.from({length:10_000},(_,i)=>({id:i,name:`Item ${i+1}`}))
const virt=useVirtualizer({
count: items.length,
getScrollElement: ()=>parentRef.value,
estimateSize: ()=>40,
overscan: 5,
})
const vRows = computed(()=>virt.value.getVirtualItems())
const totalH = computed(()=>virt.value.getTotalSize())
</script>
<template>
<div ref="parentRef" style="height:500px;overflow-y:auto;">
<div :style="{ height:`${totalH}px`, position:'relative' }">
<div v-for="vr in vRows" :key="vr.key"
:style="{ position:'absolute',top:0,
transform:`translateY(${vr.start}px)`,
height:`${vr.size}px`,width:'100%' }">
{{ items[vr.index].name }}
</div>
</div>
</div>
</template>
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
