Created
November 28, 2024 07:42
-
-
Save kuc-arc-f/36da8129887d5656abaf5c9bc13cdbbd to your computer and use it in GitHub Desktop.
react-router-7 , example
This file contains hidden or 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 type { Route } from "./+types/home"; | |
import { Form , useSubmit } from "react-router"; | |
export function meta({}: Route.MetaArgs) { | |
return [ | |
{ title: "New React Router App" }, | |
{ name: "description", content: "Welcome to React Router!" }, | |
]; | |
} | |
export async function loader({ params }: Route.LoaderArgs) { | |
return { todos: []}; | |
} | |
export async function action({ | |
request, | |
}: Route.ActionArgs) { | |
let formData = await request.formData(); | |
let title = await formData.get("title"); | |
const input = { | |
title: title | |
} | |
console.log(input); | |
return {data : input }; | |
} | |
export default function Product({ | |
loaderData, | |
actionData, | |
}: Route.ComponentProps) { | |
let submit = useSubmit(); | |
const data = loaderData; | |
console.log(data); | |
console.log(actionData); | |
return ( | |
<>test1 | |
<hr /> | |
<form method="post" onSubmit={(e) => { | |
e.preventDefault(); | |
const sendFormData = new FormData(e.currentTarget); | |
submit(sendFormData, { method: "post" }); | |
}}> | |
<div> | |
<span htmlFor="title">タイトル</span> | |
<input | |
id="title" | |
name="title" | |
defaultValue="" | |
/> | |
<button type="submit">Save</button> | |
</div> | |
<hr /> | |
</form> | |
</> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment