Created
January 17, 2023 00:10
-
-
Save joetifa2003/07c1fd3b42e43427193bf4ea0d1688fa to your computer and use it in GitHub Desktop.
Svelte form loading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Form action="?/deleteTodo" let:submitting> | |
<input type="hidden" name="todoId" value={todo.id} /> | |
<button type="submit" disabled={submitting}>Delete</button> | |
</Form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@joetifa2003 FYI,
applyAction()
isn't necessary if you callupdate()
.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 touse:enhance
.