sveltekit - Svelte page.form is undefined when destructured - Stack Overflow

I am building a simple form component following the Svelte documentation, which says that I can access

I am building a simple form component following the Svelte documentation, which says that I can access the form action data from an external component using the page state.

When I destructure form from the page state, it is undefined.

However, I can access it if I do page.form directly.

Why is that? What am I missing?

<script lang="ts">
    import { enhance } from "$app/forms";
    import { page } from "$app/state";

  let pageDate = page; // pageDate.form is defined
  // let { form } = page; -> form is undefined
</script>

<h3>Login</h3>

{#if pageDate.form?.error}
  <p class="error">{pageDate.form?.error}</p>
{/if}

<form method="POST" action="?/login" use:enhance>
    <label>
        Email
        <input name="email" type="email">
    </label>
    <label>
        Password
        <input name="password" type="password">
    </label>
    <button>Log in</button>
</form>

I am building a simple form component following the Svelte documentation, which says that I can access the form action data from an external component using the page state.

When I destructure form from the page state, it is undefined.

However, I can access it if I do page.form directly.

Why is that? What am I missing?

<script lang="ts">
    import { enhance } from "$app/forms";
    import { page } from "$app/state";

  let pageDate = page; // pageDate.form is defined
  // let { form } = page; -> form is undefined
</script>

<h3>Login</h3>

{#if pageDate.form?.error}
  <p class="error">{pageDate.form?.error}</p>
{/if}

<form method="POST" action="?/login" use:enhance>
    <label>
        Email
        <input name="email" type="email">
    </label>
    <label>
        Password
        <input name="password" type="password">
    </label>
    <button>Log in</button>
</form>
Share Improve this question asked Mar 8 at 11:07 Loïc BosetLoïc Boset 5456 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

By destructuring, you get the property just once; it will be undefined on initial page load and stay that way.

You can make the destructuring reactive via $derived:

let { form } = $derived(page);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744897684a4599793.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信