Created
February 24, 2023 00:18
-
-
Save jaroel/407eacf81a7d752fb0924e19d304bcda to your computer and use it in GitHub Desktop.
Use POST data in forms in SSR with SolidStart
This file contains 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
import {isServer} from 'solid-js/web' | |
import {createRouteData} from 'solid-start' | |
import {useRequest} from 'solid-start/server' | |
export default function FormPage() { | |
const postData = createRouteData(async () => { | |
const event = useRequest() | |
if (!isServer || event.request.method !== 'POST') { | |
return undefined | |
} | |
const data = await event.request.formData() | |
return Object.fromEntries(data) as Record<string, string> | |
}) | |
return <> | |
<h1>SSR-only Form POST data</h1> | |
<form method='post' enctype='multipart/form-data'> | |
<input type='hidden' name='subject' value={postData()?.subject ?? 'Defense against the Dark Arts'} /> | |
<input type='text' name='myfield' class='border' value={postData()?.myfield ?? ''} /> | |
<button type='submit'>Submit</button> | |
<pre>{JSON.stringify(postData())}</pre> | |
</form> | |
</> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment