Get Request Inside Afterupdate Causes an Infinitive Loop Svelte

I am doing a get request on Svelte inside an after Update function. This creates an infinitive loop of get requests. How can I only get one request after a certain element is updated ?

For example in this case I am getting a list ,and I am using the afterUpdate because there is an option to delete lists and I want the user to see the updated list directly not after refreshing or navigating to other screens.

Or if there is another way except afterUpdate function to fulfill my purpose please let me know


 afterUpdate(async () => {
    const resp = await fetch(`__________/list`, {

    });

    if (resp.ok) {
      openlist = await resp.json();

    } else if (resp.data === "") {
      throw new Error("Could not find any company");
    }
  });

1 Answer

You could probably just use a reactive statement to trigger the fetch. E.g.

async function update() {
    // fetch here
}

// Whatever thing whose change should trigger the fetch
let someDependency;

$: someDependency, update();

(The update logic should not be inlined, otherwise the properties that are set within the function will also be tracked and you will potentially end up with a loop again.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest