Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[React 19] Can't use useActionState (NextJS) #29017

Closed
fernandortec opened this issue May 7, 2024 · 11 comments
Closed

[React 19] Can't use useActionState (NextJS) #29017

fernandortec opened this issue May 7, 2024 · 11 comments
Labels

Comments

@fernandortec
Copy link

I get this error, trying to useActionState in React19 and NextJS, I followed the documentation and tried using several approaches, but none worked, here's the code and the React Version below

image

"use client";

import { useActionState } from "react";
import * as S from "./styles";

interface PurchaseFormProps {
	priceId: string;
}

export function PurchaseForm({ priceId }: PurchaseFormProps): JSX.Element {
	const [state, formAction] = useActionState(() => {}, null);

	return (
		<form className={S.form}>
			<button className={S.formButton} type="submit" disabled={true}>
				Comprar agora
			</button>
		</form>
	);
}

"react": "^19.0.0-beta-e7d213dfb0-20240507", "react-dom": "^19.0.0-beta-1beb73de0f-20240503",

@rickhanlonii
Copy link
Member

Next vendors React so you can't install your own version of React, it forces you to use the vendor version. Next will add React 19 to a future release.

@rickhanlonii rickhanlonii closed this as not planned Won't fix, can't repro, duplicate, stale May 7, 2024
@ph1p
Copy link

ph1p commented May 8, 2024

I completely understand the confusion as I received the same error today. This is a little bit misleading as nextjs uses these functions without any context in the examples https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations#examples

Or am I missing something?

@fernandortec you can use useFormState from react-dom. This will later be removed and replaced by useActionState.

actions

"use server";

export async function submitForm(previousState, formData) {
  return {
      name: formData.get("name"),
  }
}

form component

"use client";

import { useFormState } from "react-dom";

// ...
const [state, formAction] = useFormState(submitForm, {
  name: "",
});

// ...
<form action={formAction}>
  <label htmlFor="name">Name</label>
  <input type="text" name="name" />
  <button type="submit">submit</button>
</form>
// ...

@eps1lon
Copy link
Collaborator

eps1lon commented May 8, 2024

[email protected] should have support for React.useActionState

@PolyagaDev
Copy link

Je comprends parfaitement la confusion car j'ai reçu la même erreur aujourd'hui. C'est un peu trompeur car nextjs utilise ces fonctions sans aucun contexte dans les exemples https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations#examples

Ou est-ce que j'ai raté quelque chose ?

@fernandortecvous pouvez utiliser useFormStateà partir de react-dom. Celui-ci sera ensuite supprimé et remplacé par useActionState.

Actions

"use server";

export async function submitForm(previousState, formData) {
  return {
      name: formData.get("name"),
  }
}

composant de formulaire

"use client";

import { useFormState } from "react-dom";

// ...
const [state, formAction] = useFormState(submitForm, {
  name: "",
});

// ...
<form action={formAction}>
  <label htmlFor="name">Name</label>
  <input type="text" name="name" />
  <button type="submit">submit</button>
</form>
// ...

have you found a solution? Next makes me use useActionState which is literally nowhere to be found in my 'react' folder. I have version 19.0.0 of react with the latest version of next.js but nothing works.

@gabbas5
Copy link

gabbas5 commented Jun 21, 2024

I am also in the same state and cannot use "useActionState" hook, I am trying to find out alternate solution.I don't know why they have mentioned this and not tested its working with nextjs 14

@MarcGruener
Copy link

MarcGruener commented Jun 22, 2024

@gabbas5 as @ph1p suggests, you should be able to simply replace
import { useActionState } from 'react';
with
import { useFormState } from 'react-dom';

@smolpaw
Copy link

smolpaw commented Jun 24, 2024

@gabbas5 as @ph1p suggests, you should be able to simply replace import { useActionState } from 'react'; with import { useFormState } from 'react-dom';

This worked and the build passed. However it results in this error during development

Warning: ReactDOM.useFormState has been renamed to React.useActionState. Please update EditInvoiceForm to use React.useActionState.

@riccardolardi
Copy link

Is there any approx release date for React 19 and Next.js allowing to use useActionState ?

@alexmondaini
Copy link

does react 19 RC comes with useActionState ?

@gabbas5
Copy link

gabbas5 commented Sep 6, 2024 via email

@rbzdev
Copy link

rbzdev commented Sep 16, 2024

Run the following command to install the good version of react and react-dom canary, who support useActionState hook with Next JS server action : pnpm install [email protected] [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests