Skip to content

Commit 55df273

Browse files
committed
A note on form submission with <WhenVisible> components
1 parent dbb035f commit 55df273

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

resources/js/Pages/load-when-visible.jsx

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const meta = {
77
{ url: '#top', name: 'Introduction' },
88
{ url: '#loading-before-visible', name: 'Loading before visible' },
99
{ url: '#always-trigger', name: 'Always trigger' },
10+
{ url: '#form-submissions', name: 'Form submissions' },
1011
],
1112
}
1213

@@ -414,6 +415,135 @@ export default function () {
414415
},
415416
]}
416417
/>
418+
<H2>Form submissions</H2>
419+
<P>
420+
When submitting forms, you may want to use the <Code>except</Code> option to exclude the props that are being
421+
used by the <Code>WhenVisible</Code> component. This prevents the props from being reloaded when you get
422+
redirected back to the current page because of validation errors.
423+
</P>
424+
<TabbedCode
425+
examples={[
426+
{
427+
name: 'Vue',
428+
language: 'markup',
429+
code: dedent`
430+
<script setup>
431+
import { useForm, WhenVisible } from '@inertiajs/vue3'
432+
433+
const form = useForm({
434+
name: '',
435+
email: '',
436+
})
437+
438+
function submit() {
439+
form.post('/users', {
440+
except: ['permissions'],
441+
})
442+
}
443+
</script>
444+
445+
<template>
446+
<form @submit.prevent="submit">
447+
<!-- ... -->
448+
</form>
449+
450+
<WhenVisible data="permissions">
451+
<!-- ... -->
452+
</WhenVisible>
453+
</template>
454+
`,
455+
},
456+
{
457+
name: 'React',
458+
language: 'jsx',
459+
code: dedent`
460+
import { useForm, WhenVisible } from '@inertiajs/react'
461+
462+
export default function CreateUser() {
463+
const { data, setData, post } = useForm({
464+
name: '',
465+
email: '',
466+
})
467+
468+
function submit(e) {
469+
e.preventDefault()
470+
post('/users', {
471+
except: ['permissions'],
472+
})
473+
}
474+
475+
return (
476+
<>
477+
<form onSubmit={submit}>
478+
{/* ... */}
479+
</form>
480+
481+
<WhenVisible data="permissions">
482+
{/* ... */}
483+
</WhenVisible>
484+
</>
485+
)
486+
}
487+
`,
488+
},
489+
{
490+
name: 'Svelte 4',
491+
language: 'jsx',
492+
code: dedent`
493+
<script>
494+
import { useForm, WhenVisible } from '@inertiajs/svelte'
495+
496+
const form = useForm({
497+
name: '',
498+
email: '',
499+
})
500+
501+
function submit() {
502+
$form.post('/users', {
503+
except: ['permissions'],
504+
})
505+
}
506+
</script>
507+
508+
<form on:submit|preventDefault={submit}>
509+
<!-- ... -->
510+
</form>
511+
512+
<WhenVisible data="permissions">
513+
<!-- ... -->
514+
</WhenVisible>
515+
`,
516+
},
517+
{
518+
name: 'Svelte 5',
519+
language: 'jsx',
520+
code: dedent`
521+
<script>
522+
import { useForm, WhenVisible } from '@inertiajs/svelte'
523+
524+
const form = useForm({
525+
name: '',
526+
email: '',
527+
})
528+
529+
function submit() {
530+
form.post('/users', {
531+
except: ['permissions'],
532+
})
533+
}
534+
</script>
535+
536+
<form onsubmit={submit}>
537+
<!-- ... -->
538+
</form>
539+
540+
<WhenVisible data="permissions">
541+
<!-- ... -->
542+
</WhenVisible>
543+
`,
544+
},
545+
]}
546+
/>
417547
</>
418548
)
419549
}

0 commit comments

Comments
 (0)