Next.js Discord

Discord Forum

revalidatePath() not working

Unanswered
Barbary Lion posted this in #help-forum
Open in Discord
Barbary LionOP
export const updateUser = actionClient .schema(updateUserSchema, { handleValidationErrorsShape: (ve) => flattenValidationErrors(ve).fieldErrors, }) .action(async ({ parsedInput }) => { const currentUser = await getCurrentUser(); const updateData = {} as Pick<User, "phone" | "username">; if (!currentUser) { returnValidationErrors(updateUserSchema, { _errors: ["You must be logged in to update your account"], }); } if (parsedInput.phone) updateData.phone = parsedInput.phone; if (parsedInput.username) updateData.username = parsedInput.username; // if (user.dateOfBirth) updateData.dateOfBirth = user.dateOfBirth; try { await db .update(userTable) .set(updateData) .where(eq(userTable.id, currentUser.id!)); } catch (err) { return { success: false, content: { _errors: [err], }, }; } revalidatePath("/settings/profile"); return { success: true, content: parsedInput, }; });

  const { form, action, handleSubmitWithAction, resetFormAndAction } =
    useHookFormAction(updateUser, zodResolver(updateUserSchema), {
      actionProps: {
        onSuccess: () => {
          toast({
            title: "Success!",
            description: "User updated successfully",
          });
          resetFormAndAction();
        },
        onError: () => {
          toast({
            title: "Something went wrong!",
            description: "Your update request failed. Please try again.",
            variant: "destructive",
          });
        },
      },
      formProps: {
        mode: "onSubmit",
      },
    });


after successful form submission fields reset to undefined but UI doesnt update it and still shows pre-submitted values. I think revalidatePath should fix that but it is not

16 Replies

do you have your form and user info in the same page?
what's your ui like?
@James4u what's your ui like?
Barbary LionOP
first is client component rendering ui and handles the action
second is server action wrapped with next-safe-action
no my question is, where do you display user info (that you submit from your form)?
is it in the same page where your form is? or do you show that info in the different page?
"/settings/profile"
oh, I see
then you want to show updated user info in your form?
Barbary LionOP
i want the field to actually display reset values, since on successful submit i clear them using reset function provided by react-hook-form. The fields resets after submit, but UI doesnt update and I see the values i submited in inputs
Am I confusing revalidatePath() functionality? I ain't sure anymore if it's a right way to handle that
how are you getting and setting the default values?
@sergiy.eth how are you getting and setting the default values?
Barbary LionOP
defaults are set automatically, if you don't set them explicitly
issue is fixed. It works now, even with no resetFormAndAction() call. Just revalidatePath started working suddenly, after 1 hour..