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.)