Web / TanStack Interview Questions
How do you virtualise a horizontal list or two-dimensional grid with TanStack Virtual?
TanStack Virtual supports vertical (default), horizontal (horizontal:true), and grid layouts using two virtualizers — one per axis. Only the cells whose row AND column are both in view are rendered.
<script setup lang="ts">
import { useVirtualizer } from "@tanstack/vue-virtual"
import { ref, computed } from "vue"
const parentRef=ref<HTMLDivElement|null>(null)
const ROW_COUNT=1000, COL_COUNT=50
const rowVirt=useVirtualizer({
count: ROW_COUNT,
getScrollElement: ()=>parentRef.value,
estimateSize: ()=>35,
overscan: 5,
})
const colVirt=useVirtualizer({
horizontal: true,
count: COL_COUNT,
getScrollElement: ()=>parentRef.value,
estimateSize: ()=>120,
overscan: 5,
})
const vRows=computed(()=>rowVirt.value.getVirtualItems())
const vCols=computed(()=>colVirt.value.getVirtualItems())
const totW =computed(()=>colVirt.value.getTotalSize())
const totH =computed(()=>rowVirt.value.getTotalSize())
</script>
<template>
<div ref="parentRef" style="height:400px;width:800px;overflow:auto;">
<div :style="{ height:`${totH}px`, width:`${totW}px`, position:'relative' }">
<div v-for="vr in vRows" :key="vr.key"
:style="{ position:'absolute',top:0,
transform:`translateY(${vr.start}px)`,width:'100%' }">
<div v-for="vc in vCols" :key="vc.key"
:style="{ position:'absolute',left:0,
transform:`translateX(${vc.start}px)`,
width:`${vc.size}px`,height:`${vr.size}px` }">
Cell ({{vr.index}},{{vc.index}})
</div>
</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...
