Created
July 6, 2023 19:13
-
-
Save leomiranda/4e60750aed221713d490c09f04158379 to your computer and use it in GitHub Desktop.
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 { ActionArgs, json } from '@remix-run/node'; | |
import { useActionData, useSubmit } from '@remix-run/react'; | |
import { useState } from 'react'; | |
export async function action({ request }: ActionArgs) { | |
const body = await request.formData(); | |
const data = JSON.parse(body.get('data') as string); | |
const firstName = data?.user?.first_name; | |
return json({ message: `Hello, ${firstName}` }); | |
} | |
export default function Test() { | |
const submit = useSubmit(); | |
const data = useActionData<typeof action>(); | |
const [dataForm] = useState({ | |
user: { | |
first_name: 'Leonardo', | |
}, | |
}); | |
function handleOnClick() { | |
submit({ data: JSON.stringify(dataForm) }, { method: 'post' }); | |
} | |
return ( | |
<div className="text-center m-20"> | |
<button onClick={handleOnClick} className="bg-primary text-lg p-4"> | |
Click here | |
</button> | |
<p className="text-red">{data?.message}</p> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment