-
Hello everyone! I started using the useActionData() hook for the first time, May I ask where I did wrong? Thanks for your answer. https://codesandbox.io/s/relaxed-blackwell-g7qjky?file=/src/my-component.tsx import {
ActionFunctionArgs,
json,
useActionData,
useFetcher
} from "react-router-dom";
export function Component() {
const fetcher = useFetcher();
const actionData = useActionData(); // undefined
console.log(actionData);
return (
<div>
<button
onClick={() => {
fetcher.submit(
{ msg: "this is submit data" },
{ method: "POST", encType: "application/json" }
);
}}
>
Submit
</button>
</div>
);
}
export async function action({ request }: ActionFunctionArgs) {
const data = await request.json();
console.log(`action function run`, { ...data, method: request.method });
return json(
{
msg: "this is action result"
},
{ status: 200 }
);
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 18 replies
-
I should use |
Beta Was this translation helpful? Give feedback.
-
I just came across the same problem. The more confusing thing is that it works on one of my routes in which the form is being submitted via a button, but on the other route (which has the same setup) it just constantly returns Any more info on this? |
Beta Was this translation helpful? Give feedback.
-
@kiliman could you explain why I can see data inside action then? basically I am submitting data from nested route to a parent parent
patent/child-route the data is visible inside the action but it can't be retrieved using |
Beta Was this translation helpful? Give feedback.
When you
useFetcher
the returned data is on the fetcher instance.useActionData()
only returns the data when you use<Form method="post">
oruseSubmit()