Skip to content
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

IntersectionObserver inside Svelte #each block causes infinite loop #69

Open
matthew-ia opened this issue Apr 16, 2023 · 2 comments
Open

Comments

@matthew-ia
Copy link

matthew-ia commented Apr 16, 2023

Not sure if this is an expected limitation or a bug, or perhaps specific to my project, but I get an infinite loop that locks up the app when I place <IntersectionObserver> into an #each block:

<script>
  import IntersectionObserver from 'svelte-intersection-observer';
  import Some from '$components/some.svelte';

  const sections = [
    {
      id: 'some',
      node: undefined,
      intersecting: undefined,
      component: Some,
    },
  ];
</script>

{#each sections as {id, node, intersecting, component}}
  <IntersectionObserver element={node} bind:intersecting>
    <section 
      id={id} 
      class:intersecting={intersecting} 
      bind:this={node}
    >
      <svelte:component this={component} />
    </section>
  </IntersectionObserver>
{/each}

Related: #63

@lvnam96
Copy link

lvnam96 commented Apr 21, 2023

your code should work
checkout this REPL example I made based on your code (just add threshold option, background & text color for better visualization while scrolling): https://svelte.dev/repl/60d4410aae7c48fb96b68b220e3495f8?version=3

@TrenchTown
Copy link

TrenchTown commented Apr 20, 2024

I can't understand why the script is triggered only when the very last element id=3 is reached and puts the class "intersecting" on all 3 elements at once?

Where is my mistake?


<script lang="ts">
    import IntersectionObserver from "svelte-intersection-observer";

    let intersecting: boolean;
    let node: any;

    const data = [
        {
            id: 1,
            text: "aa",
        },
        {
            id: 2,
            text: "aa",
        },
        {
            id: 3,
            text: "aa",
        },
    ];
</script>

<style>
    .item {height: 50vh; }
</style>

<main>
    {#each data as item}
        <IntersectionObserver element={node} bind:intersecting threshold={0.7}>
            <section id="{item.id}" class:intersecting bind:this={node}>
                <div class="item">{item.text}</div>
            </section>
        </IntersectionObserver>
    {/each}
</main>

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

No branches or pull requests

3 participants