Skip to content

feat: refactor to move instantiating LoaderState to the user #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2025

Conversation

ndom91
Copy link
Owner

@ndom91 ndom91 commented Mar 13, 2025

Description

  • Refactored loaderState to no longer export an instance of LoaderState, but the class itself
  • This means the user must instantiate the LoaderState class in their component and pass it into the InfiniteLoader component as a prop also called loaderState
    • See updated examples in the README as well as src/routes/+page.svelte

Linked Issues

Fixes #6

Migrating

Migration consists of the following three steps

  1. Modify your import to point at the uppercase LoaderState class
  2. Instantiate an instance of LoaderState yourself
  3. Pass that instance into the InfiniteLoader component as a prop
<script lang="ts">
+  import { InfiniteLoader, LoaderState } from "svelte-infinite"
-  import { InfiniteLoader, loaderState } from "svelte-infinite"

+  const loaderState = new LoaderState()
  const allItems = $state([])

  const loadMore = async () => {
    const res = fetch("...")
    const data = await res.json()
    allItems.push(...data)
    loaderState.loaded()
  }
</script>

+ <InfiniteLoader {loaderState} triggerLoad={loadMore}>
- <InfiniteLoader triggerLoad={loadMore}>
  {#each allItems as user (user.id)}
    <div>{user.name}</div>
  {/each}
</InfiniteLoader>

Copy link

vercel bot commented Mar 13, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
svelte-infinite ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 13, 2025 0:14am

@ndom91 ndom91 mentioned this pull request Mar 13, 2025
3 tasks
@ndom91 ndom91 merged commit c8ce22d into main Mar 19, 2025
2 checks passed
@ndom91 ndom91 deleted the ndom91/loaderState-per-instance branch March 19, 2025 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Having to manually reset loaderState
1 participant