UseFormStatus not working.
Answered
Yellow croaker posted this in #help-forum
Yellow croakerOP
to get useFormStatus to work should i use canary versio or what kind versio from react, because
i get
There is no indication in docs that i should use canary or what version from react.
allthought i'm using
seems that using experimental_useFormStatus as useFormStatus removes the error.
But i get this error in my browser:
Component looks like this:
i get
'"react-dom"' has no exported member named 'useFormStatus'. Did you mean 'FormStatus'?ts(2724)
import useFormStatusThere is no indication in docs that i should use canary or what version from react.
allthought i'm using
"@types/react": "18.2.19",
"@types/react-dom": "^18.2.7",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"next": "^14.0.0",seems that using experimental_useFormStatus as useFormStatus removes the error.
But i get this error in my browser:
Unhandled Runtime Error
TypeError: react_dom__WEBPACK_IMPORTED_MODULE_2__.experimental_useFormStatus is not a functionComponent looks like this:
import { deleteWorkExperience } from '@/app/lib/actions';
import { TrashIcon } from 'lucide-react';
import { experimental_useFormStatus as useFormStatus } from 'react-dom';
function DeleteButton() {
const { pending } = useFormStatus();
return (
<button
type="button"
className="rounded-md border p-2 hover:bg-gray-100"
disabled={pending}
>
<span className="sr-only">Delete</span>
<TrashIcon className="w-4" />
</button>
);
}
export function DeleteExperienceButton({
id,
name,
}: {
id: string;
name: string;
}) {
console.log('id', id);
console.log('name', name);
const deleteExperience = async (id: string) => {
const response = await deleteWorkExperience.bind(null, id);
console.log('value', response);
};
return (
<form action={deleteExperience(id)}>
<DeleteButton />
<span className="sr-only">Delete</span>
</form>
);
}Answered by Yellow croaker
updated to latest react/types But i still seems to get the same error:
'"react-dom"' has no exported member named 'useFormStatus'. Did you mean 'FormStatus'?ts(2724)
'"react-dom"' has no exported member named 'useFormStatus'. Did you mean 'FormStatus'?ts(2724)
32 Replies
https://react.dev/reference/react-dom/hooks/useFormStatus
You can use this version: https://ibb.co/mCGGxZ7
You can use this version: https://ibb.co/mCGGxZ7
its basicly because react doesn't have their own types so you have to rely on @types/ for TS
and that isn't managed by react, so kinda slow
Yellow croakerOP
Allright! This question got a bit crossposted in general sorry! But yeah i will then install canary version from react, because the experimental tag is not working for some reason.
im pretty sure the version of react in nextjs doesn't actually matter as they bundle the right version in next.js anyway
Yellow croakerOP
So should the experimental tag work, or is there some sort of bug?
Yellow croakerOP
Yes i think so, now i can import
import { useFormStatus } from 'react-dom';
but yeah the types are not there, so i just probably eslint ignore it?
import { useFormStatus } from 'react-dom';
but yeah the types are not there, so i just probably eslint ignore it?
'"react-dom"' has no exported member named 'useFormStatus'. Did you mean 'FormStatus'?ts(2724)but the thing i linked added it to tsconfig , so you should be able to use it?
Yellow croakerOP
ye added it
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"types":["react-dom/canary"],
....but it still was not working ;(((
Yellow croakerOP
well atleast its not crashing now 🙂 but it says the error about the exporter member
@riský but it still was not working ;(((
Yellow croakerOP
This question vaguely related to the useFormStatus.
When clicking deleteButton my form doesnt get called, because i get this kind error:
Is this because my parent component is using useForm where this deleteExperiecneButton is used?
So i cant really if the pending status from UseFormStatus is working😕
'use client';
import { deleteWorkExperience } from '@/app/lib/actions';
import { TrashIcon } from 'lucide-react';
import { useFormStatus, useFormState } from 'react-dom';
function DeleteButton() {
const { pending } = useFormStatus();
return (
<button type="submit" aria-disabled={pending}>
Delete
</button>
);
}
export function DeleteExperienceButton({
field,
name,
}: {
field: string;
name: string;
}) {
const id = field.field.id;
// const [state, formAction] = useFormState(deleteWorkExperience, initialState);
const deleteExperience = deleteWorkExperience.bind(null, id);
return (
<form action={deleteExperience}>
<input type="hidden" name="id" value={id} />
<DeleteButton />
<span className="sr-only">Delete</span>
</form>
);
}When clicking deleteButton my form doesnt get called, because i get this kind error:
Uncaught Error: A React form was unexpectedly submitted. If you called form.submit() manually, consider using form.requestSubmit() instead. If you're trying to use event.stopPropagation() in a submit event handler, consider also calling event.preventDefault().Is this because my parent component is using useForm where this deleteExperiecneButton is used?
<Form {...workExperienceForm}>
<form onSubmit={handleSubmit(onSubmit)} className="space-y-8">
<div className=>
...
<Button className="btn btn-primary mr-1">Submit</Button>
<DeleteExperienceButton field={field} name={'workExperience'} />
</div>
</form>
</Form>So i cant really if the pending status from UseFormStatus is working😕
ohh i forgot it wasn't just a type error
but idk about nexting your forms tbh
Yellow croakerOP
Yeah im trying to follow the Nextjs Learn section form example
https://nextjs.org/learn/dashboard-app/mutating-data
Bottom of the page is the example
https://nextjs.org/learn/dashboard-app/mutating-data
Bottom of the page is the example
but that isn't nesting the forms right? (like having one inside other)
Yellow croakerOP
Not sure what u mean by nesting here?
oh right! so the forms (buttons) are nested wrongly?
@Yellow croaker Not sure what u mean by nesting here?
like your paranet compoent has a form, and your button commonent also has one, and im not sure if they work well nested
also is the first form for react-hook-form?
Yellow croakerOP
Oh right that might bring problems yes. If i submit the parent, it might have some side effects to the children form.
Yes first is RHF
Yes first is RHF
WorkExperiences.tsx
*Tyotesti.tsx
DeleteExperienceButton.tsx
Here is how the form is structured
Accordion type="single" collapsible>
{workexperiences.map((field, index) => {
console.log('WorkExperience', field);
return (
<div key={field.id}>
<AccordionItem value={`item-${index}`}>
<AccordionTrigger>
<div> {field.company}</div>{' '}
</AccordionTrigger>
<AccordionContent>
<Tyotesti field={field} index={index} />
</AccordionContent>
</AccordionItem>
</div>
);
})}
</Accordion>*Tyotesti.tsx
<Form {...workExperienceForm}>
<form onSubmit={handleSubmit(onSubmit)} className="space-y-8">
<div className=>
...
<Button className="btn btn-primary mr-1">Submit</Button>
<DeleteExperienceButton field={field} name={'workExperience'} />
</div>
</form>
</Form>DeleteExperienceButton.tsx
'use client';
import { deleteWorkExperience } from '@/app/lib/actions';
import { TrashIcon } from 'lucide-react';
import { useFormStatus, useFormState } from 'react-dom';
function DeleteButton() {
const { pending } = useFormStatus();
return (
<button type="submit" aria-disabled={pending}>
Delete
</button>
);
}
export function DeleteExperienceButton({
field,
name,
}: {
field: string;
name: string;
}) {
const id = field.field.id;
// const [state, formAction] = useFormState(deleteWorkExperience, initialState);
const deleteExperience = deleteWorkExperience.bind(null, id);
return (
<form action={deleteExperience}>
<input type="hidden" name="id" value={id} />
<DeleteButton />
<span className="sr-only">Delete</span>
</form>
);
}Here is how the form is structured
But yeah might be that i cant use useFormStatus here. Probably just gotta use state to check if its loading.
Asian black bear
I had this type of error but with useOptimistic the problem is from @types/react it should be above 18.2.26
you have 18.2.19 so just updated to its latest it will work
@Asian black bear I had this type of error but with useOptimistic the problem is from @types/react it should be above 18.2.26
Yellow croakerOP
updated to latest react/types But i still seems to get the same error:
'"react-dom"' has no exported member named 'useFormStatus'. Did you mean 'FormStatus'?ts(2724)
'"react-dom"' has no exported member named 'useFormStatus'. Did you mean 'FormStatus'?ts(2724)
Answer
Upgrade
@types/react-dom to latest tooThis definition is in react-dom not react
And btw you don’t need to modify tsconfig. Revert all changes to tsconfig
@joulev And btw you don’t need to modify tsconfig. Revert all changes to tsconfig
Yellow croakerOP
Worked out thanks!