React router dom Actions

  Рет қаралды 475

forinda dev

forinda dev

Күн бұрын

Пікірлер: 11
@ur.cristinanor
@ur.cristinanor 2 жыл бұрын
Thanks for the efforts bro, have you tried out using useFetcher to submit data from a checkbox. I am really failling to come up with this.
@forinda
@forinda 2 жыл бұрын
I'll do an upload on this if you need. Still haven't used it but I can do one on top of this
@ur.cristinanor
@ur.cristinanor 2 жыл бұрын
@@forinda for sure
@forinda
@forinda 2 жыл бұрын
Uploading a video on useFetcher today
@ur.cristinanor
@ur.cristinanor 2 жыл бұрын
@@forinda You are a gem brother, let me check it out.
@eliediab3091
@eliediab3091 Жыл бұрын
Thanks for this! One question though, if I'm displaying the add post form and the posts in the same page, is there a way to clear the inputs when submitting?
@forinda
@forinda Жыл бұрын
If you want to reset your form, You'll not now rely on the default form submit. Use the useSubmit hook to do the form submittion I have this simple setup: import React from "react"; import { ActionFunctionArgs, Form, useActionData, useRouteError, useSubmit, } from "react-router-dom"; const Home = () => { const data = useActionData() as { title: string }; const submit = useSubmit(); return ( Home { e.preventDefault(); submit(e.currentTarget.form); e.currentTarget.form!.reset(); }} > Submit ); }; export default Object.assign(Home, { action: async ({ request }: ActionFunctionArgs) => { const formData = await request.formData(); const data = Object.fromEntries(formData.entries()); console.log(data); return null; }, ErrorElement: () => { const error = useRouteError() as any; return ( Error {error.statusText} ); }, }); In the above submit button can get the current target thus you can access the form object and reset after submission. Above still before submission you can carry out validation before submission then after successful entry you can reset your form using this simple code onClick={(e) => { e.preventDefault(); submit(e.currentTarget.form); e.currentTarget.form!.reset(); }} I hope this helps. If you need more clarification you can just ask below☺
@eliediab3091
@eliediab3091 Жыл бұрын
@@forinda yup this works!! thank you so much!
@eliediab3091
@eliediab3091 Жыл бұрын
Brother I have one more question, is it really possible to read and add data to react context in loader and action functions? since we can't use hooks inside of them. Thank you once again.
@forinda
@forinda Жыл бұрын
@@eliediab3091 It will throw an exception for sure because that will be outside the scope of the jsx component
@forinda
@forinda Жыл бұрын
Just handle every logic within your components and use the useActiondata hook to retrieve values from your actions
React router dom useFetcher
19:57
forinda dev
Рет қаралды 1,2 М.
React Router in Depth #10 - Forms & Actions
11:17
Net Ninja
Рет қаралды 36 М.
So Cute 🥰 who is better?
00:15
dednahype
Рет қаралды 19 МЛН
人是不能做到吗?#火影忍者 #家人  #佐助
00:20
火影忍者一家
Рет қаралды 20 МЛН
FASTER JavaScript In 2025 With Sets
13:13
Jack Herrington
Рет қаралды 19 М.
[Golang] 007 - Hello World
12:54
Go With Habib
Рет қаралды 1,2 М.
Netflix Removed React?
20:36
Theo - t3․gg
Рет қаралды 60 М.
React Tutorial for Beginners
1:20:04
Programming with Mosh
Рет қаралды 3,7 МЛН
So Cute 🥰 who is better?
00:15
dednahype
Рет қаралды 19 МЛН