Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save leomiranda/4e60750aed221713d490c09f04158379 to your computer and use it in GitHub Desktop.
Save leomiranda/4e60750aed221713d490c09f04158379 to your computer and use it in GitHub Desktop.
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