Hello everyone,
I’m using NextUI for the first time
I’m facing an issue with my edit forms. When I edit an element and go back to the edit page, the defaultValue doesn't update. I'm sharing a piece of my code, and I feel like it’s due to the rerender not happening for the defaultValue. Does anyone have a solution? It keeps the old value
However, when I use a regular input without NextUI, it works.
export default function EditCompany() {
const params = useParams();
const companyId = params.companyId as string;
const {
data: company,
isLoading,
isError } = useQuery<CompanyResp, AxiosError>({
queryKey: ['company', companyId],
queryFn: () => CompanyApi.get(companyId),
enabled: !!companyId,
retry: false,
});
if (isLoading) {
return (
<div className='mx-auto my-10 flex w-full max-w-[95rem] flex-col gap-4 px-4 lg:px-6'>
<Spinner />
</div>
);
}
if (isError) {
notFound();
}
return <CompanyEdit company={company} />;
}
export default function CompanyEdit({ company }: CompanyEditProps) {
if (!company) {
notFound();
}
const [errors, setErrors] = useState({});
const router = useRouter();
const { mutate: createCompany, isPending } = useMutation({
mutationFn: (body: EditCompanyParams) =>
CompanyApi.update(company.id, body),
onSuccess: (data: EditCompanyResp) => {
router.push(ROUTES.ADMIN.COMPANIES.index);
toast.success(data.message);
},
onError: (error: AxiosError) => handleApiErrors(error, setErrors),
});
const onSubmit = (e: React.FormEvent<HTMLFormElement>): void => {
e.preventDefault();
const formData = new FormData(e.target as HTMLFormElement);
const formValues = Object.fromEntries(formData.entries()) as {
[key: string]: string;
};
createCompany({ name: formValues.name });
};
return (
<div className='mx-auto my-10 flex w-full max-w-[95rem] flex-col gap-4 px-4 lg:px-6'>
<h3 className='text-xl font-semibold'>Edit Company</h3>
<div className='mx-auto w-full max-w-[95rem]'>
<Form
className='flex w-full max-w-xs flex-col gap-4'
validationErrors={errors}
onSubmit={onSubmit}
>
<Input
label='Name'
labelPlacement='inside'
name='name'
defaultValue={company?.name}
/>
<Button isLoading={isPending} color='primary' type='submit'>
Submit
</Button>
</Form>
</div>
</div>
);
}