• Stars
    star
    111
  • Rank 314,510 (Top 7 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Vue composition function that makes infinite scroll a breeze.

vue-use-infinite-scroll

Installation

npm i -S vue-use-infinite-scroll

Usage

template

<div>
    <span>{{ errorMessageRef }}</span>
    <ul>
        <li v-for="item in itemsRef" :key="item.id">{{ item }}</li>

        <!-- DOM element used as trigger -->
        <div ref="intersectionTrigger"></div>
    </ul>
</div>

script

import { ref, watch } from 'vue'
import { makeUseInfiniteScroll } from 'vue-use-infinite-scroll'

export default {
    setup() {
        // INTERSECTION OBSERVER

        // set the intersection options object
        // https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API
        const useInfiniteScroll = makeUseInfiniteScroll({}) // the argument is optional

        // create the template ref for the element that
        // will trigger the intersection observer
        const intersectionTrigger = ref(null) // as Ref<HTMLElement>

        // useInfiniteScroll returns a pageRef, starting from page 1,
        // which changes we should listen to fetch more data
        const pageRef = useInfiniteScroll(intersectionTrigger)

        watch(
            pageRef,
            (page) => {
                fetchItems(page)
            },
            { immediate: true }
        )

        // DATA

        const itemsRef = ref([])
        const errorMessageRef = ref('')

        async function fetchItems(page) {
            fetch(`https://jsonplaceholder.typicode.com/comments?_page=${page}&_limit=10`)
                .then((res) => res.json())
                .then((data) => itemsRef.value.push(...data))
                .catch((error) => (errorMessageRef.value = error.message))
        }

        return { intersectionTrigger, itemsRef, errorMessageRef }
    },
}

Try it here 🙂!

More Repositories

1

omniclone

An isomorphic and configurable javascript utility for objects deep cloning that supports circular references.
JavaScript
183
star
2

aww

Async Iterables Interfaces for Web Workers
JavaScript
112
star
3

strawberry

A new flexbox based CSS micro-framework.
CSS
73
star
4

oxjs

Reactive JavaScript Objects and Primitives
TypeScript
35
star
5

csp

Communicating Sequential Processes in JavaScript
TypeScript
33
star
6

asynchronousforeach

async parallel/serial foreach in JavaScript
JavaScript
30
star
7

jducers

A js transducers-like implementation using ES9
JavaScript
23
star
8

tc39-proposal-dotstructuring

Proposal: Object dotstructuring
12
star
9

list-comprehension-in-js

Haskell-like List Comprehension in JavaScript
HTML
12
star
10

Raex

Reactive Javascript using ES6 Proxies
JavaScript
12
star
11

ts-consts

Manage constants with full TypeScript support, nominal typing included!
TypeScript
9
star
12

vue-use-switch-map

A Vue composition that let you compose a ref with a function from values to refs.
TypeScript
9
star
13

recoverabletrycatch

This is a little package that brings into play a simple API similar to the `try-catch-finally` syntax to retrieve the main computation after any error was handled.
JavaScript
8
star
14

Object.setPrototypesOf

multiple inheritance using ES6 Proxy
JavaScript
6
star
15

ntfm

A node telegram bots flow manager
TypeScript
6
star
16

arraymake

A new static method for the Array constructor that let you create an array with predefined length and filling values
JavaScript
4
star
17

Promyse

Example implementation of Promises written in ES6 - not a polyfill
JavaScript
4
star
18

Supercazzole-In-TypeScript

TypeScript
3
star
19

aot-2023

My solutions for Advent of TypeScript challenges, year 2023
TypeScript
3
star
20

tc39-proposal-object-gather

Proposal: Object gather in parameters lists
2
star
21

pitogo

TypeScript
2
star
22

pitogo-web

Vue
1
star
23

jfet97

Me
JavaScript
1
star
24

progetto-ocaml-pr2

Progetto di OCaml - Esame di Programmazione II @ UniPi (2020 - 2021)
OCaml
1
star
25

JavaScript-challenges-for-truthy-masochists

Weird JavaScript challenges
JavaScript
1
star
26

resumabletcf

A package that let recover failed computations inside try blocks.
JavaScript
1
star
27

virtual-dom

Trying to build a simple virtual dom from scratch
JavaScript
1
star
28

microc

A compiler written in OCaml for the Languages, Compilers & Interpreters course @ UniPi.
OCaml
1
star
29

real_array

This template class is being created to access to the n-th element of an array using [n] instead of [n-1]. I'm creating an useless new type to replace arrays.
C++
1
star
30

ts-type-challenges

TypeScript
1
star
31

andreasimonecostadotdev

My personal blog about programming
JavaScript
1
star
32

advanced-typescript-workshop-template

Dockerfile
1
star