Next.js Discord

Discord Forum

useActionState + form examples

Answered
Lakeland Terrier posted this in #help-forum
Open in Discord
Lakeland TerrierOP
Does anyone have example code of a form component using the React19 useActionState hook requiring some zod validation in the server actions that returns error if necessary?

Bonus points for a modal input form. I learned this topic originally using the experimental hook useFormState but it appears syntax has changed and I am banging my head against a wall. Anyone have some example code I can reference?

16 Replies

Lakeland TerrierOP
And just for reference, here is my attempt at implementing this and the resulting error

import * as actions from "@/actions";
import { useActionState } from "react";

interface Props {
  id: number;
  quantity: number;
}

export default async function TheActionForm({ id, quantity }: Props) {
  const [error, theAction, isPending] = useActionState(
    async (quantity, formData) => {
      const error = await actions.theAction(id, formData.get("quantity"));
      if (error) {
        return error;
      }
      // handle success (probably just revalidate the page)
      return null;
    },
    null
  );

  return (
    <form action={theAction}>
      <input type="text" quantity={quantity} />
      <button type="submit" disabled={isPending}>
        ActionIcon
      </button>
      {error && <p>{error}</p>}
    </form>
  );
} 
 

Type error: Parameter 'quantity' implicitly has an 'any' type.
   9 | export default async function TheActionForm({ id, quantity }: Props) {
  10 |   const [error, theAction, isPending] = useActionState(
> 11 |     async (quantity, formData) => {
     |            ^
  12 |       const error = await actions.theAction(id, formData.get("quantity"));
  13 |       if (error) {
  14 |         return error;
 ELIFECYCLE  Command failed with exit code 1.
Error: Command "pnpm run build" exited with 1
Am I missing a bind somewhere? While I would like to solve my code, Id love to see how others are implementing this, and Id like to put the input fields in a little modal so when you click the icon the modal appears. (I'd like to do it with vanilla from scratch)
Lakeland TerrierOP
Am now going to watch https://www.youtube.com/watch?v=meycWptW9eo and see if it helps me out
Lakeland TerrierOP
I tried to implement using this and am really struggling. Would love to see some reference code to how it does properly
Lakeland TerrierOP
Yea I'm really struggling with the official documentation surronding useActionState, useFormState, useOptimistic.

I am just trying to take a numerical input, validate it, update the database and revalidate the page. Why is this so obtuse??
Lakeland TerrierOP
Going to try and simplify and just use the {useState} to handle to form submission as shown here https://19.react.dev/learn/reacting-to-input-with-state
Lakeland TerrierOP
I just really need some sample code to look at
Answer
Lakeland TerrierOP
Thank you my friend
after updating, the value still is zero because i don't plug a database to save the number. when you have a proper database connected it should show the correct number
Lakeland TerrierOP
So useFormState and useActionState should be completely interchangeable right now?
i am in react 19
it's a just a rename yeah, if you are already using react 19 rc you can simply use useActionState with the exact same syntax
Lakeland TerrierOP
ok