Git Product home page Git Product logo

Comments (21)

da1z avatar da1z commented on June 8, 2024 40

this is very strange to put code in docs that does not work in stable version of next

from next.js.

matthew-mcallister avatar matthew-mcallister commented on June 8, 2024 15

I was able to work around this by using these package versions:

"dependencies": {
  "next": "14.3.0-canary.59",
  "react": "19.0.0-beta-4508873393-20240430",
  "react-dom": "19.0.0-beta-4508873393-20240430"
}

Not sure what the issue is; maybe useActionState was removed from React 18.x?

from next.js.

YO-SC avatar YO-SC commented on June 8, 2024 15

For those who need it, here is an example using the useFormState hook:

the frontend

'use client'

import { someAction } from '@/app/actions'
import { useFormStatus, useFormState } from 'react-dom'

const initialState = {
  message: '',
}

export default function UploadFileForm() {
  const [state, formAction] = useFormState(someAction, initialState)

  return (
    <form className="flex flex-col gap-2" action={formAction}>
      <div className="grid w-full max-w-sm items-center gap-1.5">
        <label htmlFor="someInput">Some input</label>
        <input id="someInput" name="someInput" type="text" required />
      </div>

      <SubmitButton />

      {/* not-sr-only will make the message visible, making the element visible to sighted users as well as screen readers. */}
      <p aria-live="polite" className="not-sr-only">
        {state?.message}
      </p>
    </form>
  )
}

function SubmitButton() {
  const { pending } = useFormStatus()

  return (
    <button type="submit" disabled={pending}>
      {pending ? 'Doing form action...' : 'Do form action'}
    </button>
  )
}

the server action

'use server'
 
export async function someAction(prevState: any, formData: FormData) {
  // ...
  return {
    message: 'A message from the server action, yay!',
  }
}

thanks to @zachblume and @slimshreydy for the reminder of that hook

from next.js.

zachblume avatar zachblume commented on June 8, 2024 8

Weird. The docs should definitely be rewritten to refer to useFormState until useActionState lands in next@latest

from next.js.

slimshreydy avatar slimshreydy commented on June 8, 2024 5

Yea could we get the old useFormState example until the useActionState updates are merged to the non-canary version of Next?

from next.js.

Pmacdon15 avatar Pmacdon15 commented on June 8, 2024 5

The docs appear to have been updated to useFormState

from next.js.

matthew-mcallister avatar matthew-mcallister commented on June 8, 2024 3

Shouldn't this be reopened until some sort of solution is implemented, or at least the documentation is updated?

from next.js.

james-beston avatar james-beston commented on June 8, 2024 1

Just hit the same issue. The package in their next-forms example does not mention the fact it's a canary release, just uses the 'latest' version.

from next.js.

thedevopsguyblog avatar thedevopsguyblog commented on June 8, 2024 1

I was also stumbled by misleading documentation.
If the docs could be reverted then that would save time.
Thanks @YO-SC for the correct docs - saved me googling time.

from next.js.

thedevopsguyblog avatar thedevopsguyblog commented on June 8, 2024 1

I was able to find the correct docs by switching branch.
https://github.com/vercel/next.js/blob/v14.2.3/examples/next-forms/app/add-form.tsx

from next.js.

Pmacdon15 avatar Pmacdon15 commented on June 8, 2024

Just to note I also replaced the stater code from the base page to call the Signup component I created using the docs.

from next.js.

ashkanyadegari avatar ashkanyadegari commented on June 8, 2024

I think a bit hard to go through your repo. But I usually get this error when Im trying to use a client side hook. In order to fix this, you can just add:
"use client" to the top of the page.

See: https://nextjs.org/docs/app/building-your-application/rendering/client-components

from next.js.

Pmacdon15 avatar Pmacdon15 commented on June 8, 2024

sorry I didn't included the code I used , it is from the example the docs:

//@/app/ui/signup.tsx   
'use client'

import { useActionState } from 'react'
import { createUser } from '@/app/actions'

const initialState = {
  message: '',
}

export function Signup() {
  const [state, formAction] = useActionState(createUser, initialState)

  return (
    <form action={formAction}>
      <label htmlFor="email">Email</label>
      <input type="text" id="email" name="email" required />
      {/* ... */}
      <p aria-live="polite" className="sr-only">
        {state?.message}
      </p>
      <button>Sign up</button>
    </form>
  )
}

export default Signup

and

//@/app/actions.tsx
'use server'
import { z } from 'zod'
 
const schema = z.object({
    email: z.string({
      invalid_type_error: 'Invalid Email',
    }),
  })

export async function createUser(prevState: any, formData: FormData) {
    const validatedFields = schema.safeParse({
        email: formData.get('email'),
      })
     
      // Return early if the form data is invalid
      if (!validatedFields.success) {
        return {
          errors: validatedFields.error.flatten().fieldErrors,
        }
      }
  return {
    message: 'Please enter a valid email',
  }
}

from next.js.

Pmacdon15 avatar Pmacdon15 commented on June 8, 2024

When I looked at the react docs it says it's only available in canary, but that makes me wonder? I never knew features went backwards

from next.js.

Pmacdon15 avatar Pmacdon15 commented on June 8, 2024

I updated my dependencies to the ones you suggested and ran npm i --force and the issue was resolved. Do we know when it will make its way to the stable channel?

from next.js.

Pmacdon15 avatar Pmacdon15 commented on June 8, 2024

Okay I can reopen it. I guess you're right it would be good to update the documentation. I just figured because it's in Canary that it would be back to the stable channel shortly

from next.js.

Pmacdon15 avatar Pmacdon15 commented on June 8, 2024

I don't know if it matters but I updated the repo I provided with both solutions ( using canary, using useFormState) as branches. Thanks again for your help. I wonder if i should close this issue because it is fixed in canary and I have a work around?

from next.js.

Ucok23 avatar Ucok23 commented on June 8, 2024

this is really strange to mention a function from canary in documentation where we surely assume all its code come from latest stable release, hope they quickly add hint in documention or merge function to latest stable release

from next.js.

Pmacdon15 avatar Pmacdon15 commented on June 8, 2024

I'm assuming it will be in 14.3 but I'm not sure when that will be.

from next.js.

aidankinzett avatar aidankinzett commented on June 8, 2024

is there a stable way to know the pending state of a server action? I just want to add a loading spinner to a button but it isn't a part of a form

from next.js.

YO-SC avatar YO-SC commented on June 8, 2024

is there a stable way to know the pending state of a server action? I just want to add a loading spinner to a button but it isn't a part of a form

If you need that behaviour, what comes to my mind is to use some sort of shared/global state, pass the pending value (from the useFormState hook) to it, and use that global state value where needed.

Tools like jotai should help. As far as "backed in" nextjs solutions, i don't know of any.

This might not be the best way to do it tho. If any more devs see this and got some ideas feel free to share em.

from next.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.