Skip to content

Instantly share code, notes, and snippets.

@joetifa2003
Created January 17, 2023 00:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joetifa2003/07c1fd3b42e43427193bf4ea0d1688fa to your computer and use it in GitHub Desktop.
Save joetifa2003/07c1fd3b42e43427193bf4ea0d1688fa to your computer and use it in GitHub Desktop.
Svelte form loading
<Form action="?/deleteTodo" let:submitting>
<input type="hidden" name="todoId" value={todo.id} />
<button type="submit" disabled={submitting}>Delete</button>
</Form>
<script lang="ts">
import { applyAction, enhance } from '$app/forms';
export let action: string;
export let className = '';
let submitting = false;
</script>
<form
method="POST"
{action}
class={className}
use:enhance={() => {
submitting = true;
return async ({ update, result }) => {
await update();
await applyAction(result);
submitting = false;
};
}}
>
<slot {submitting} />
</form>
@bdougherty
Copy link

@joetifa2003 FYI, applyAction() isn't necessary if you call update(). applyAction() is only needed if you want to handle the result differently depending on what the result is. update() runs the same thing as if you didn't pass anything to use:enhance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment