Skip to content

[Typo]: In code example of server actions  #6821

Closed

Description

@patelvivekdev

Summary

In Composing Server Actions with Actions:


"use client";

import {updateName} from './actions';

function UpdateName() {
  const [name, setName] = useState('');
  const [error, setError] = useState(null);

  const [isPending, startTransition] = useTransition();

  const submitAction = async () => {
    startTransition(async () => {
      const {error} = await updateName(name);
      if (!error) {
        setError(error);
      } else {
        setName('');
      }
    })
  }
  
  return (
    <form action={submitAction}>
      <input type="text" name="name" disabled={isPending}/>
      {state.error && <span>Failed: {state.error}</span>}
    </form>
  )
}

There is no state variable here.

It should be this

    <form action={submitAction}>
      <input type="text" name="name" disabled={isPending}/>
      {error && <span>Failed: {error}</span>}
    </form>

Page

https://react.dev/reference/rsc/server-actions#composing-server-actions-with-actions

Details

No response

Activity

  1. ijayhub commented on May 27, 2024

    @ijayhub

    hello,
    is it valid?

  2. patelvivekdev commented on May 27, 2024

    @patelvivekdev
    Author
    "use server";
    
    export async function updateName(name) {
      if (!name) {
        return {error: 'Name is required'};
      }
      await db.users.updateName(name);
    }
    
    "use client";
    
    import {updateName} from './actions';
    
    function UpdateName() {
      const [name, setName] = useState('');
      const [error, setError] = useState(null);
    
      const [isPending, startTransition] = useTransition();
    
      const submitAction = async () => {
        startTransition(async () => {
          const {error} = await updateName(name);
          if (!error) {
            setError(error);
          } else {
            setName('');
          }
        })
      }
      
      return (
        <form action={submitAction}>
          <input type="text" name="name" disabled={isPending}/>
          {error && <span>Failed: {error}</span>}
        </form>
      )
    }
    
  3. devserkan commented on Jun 17, 2024

    @devserkan

    I came here to create an issue for the same code, but we already have one. I know the example is kind of pseudo-code (we are not controlling the input, there is no submit button, etc.), but even with this, it should be error-free, I guess. There is one more mistake apart from state.error, which is

    if (!error) {
      setError(error);
    ...
    

    it should be

    if (error) {
      setError(error);
    ...
    
  4. aurorascharff commented on Nov 7, 2026

    @aurorascharff
    Collaborator

    Thanks for the report and discussion! The current example renders error directly, updates the error state, and clears the name only when there is no error. Closing this as resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions