DEV Community

Cover image for useActionState β€” A New Hook in React πŸŽ‰

useActionState β€” A New Hook in React πŸŽ‰

Random on July 02, 2024

Hello Developers πŸ‘‹, It’s me your friend Md Taqui Imam, and today I’m going to explain a new and exciting hook in React called useActionState. Fo...
Collapse
 
sfritsch09 profile image
Sebastian Fritsch • Edited

It is definitely nice to use server actions and useActionState, but I still like more control over validation and still prefer Formik + Yup combo. To be honest, the end user doesn't care at the end and cannot tell the difference πŸ‘¨πŸΌβ€πŸ’»

Collapse
 
random_ti profile image
Random

Thanks for you suggestions 🫑

Collapse
 
adesoji1 profile image
Adesoji1

What's the difference between useactionstate and use effect

Collapse
 
random_ti profile image
Random

Both useActionState and useEffect are hooks in React, but they serve different purposes and are used in different scenarios

Collapse
 
joelbonetr profile image
JoelBonetR πŸ₯‡ • Edited

Just like salt and mackerel. They both are edible "things" used in different scenarios πŸ˜…

What I really meant is...

AI generated response

that we should get back the "get a life" trend but with people that use AI for everything while trying to hide it. Stop it. It's sad. If that wasn't enough its even more easy to spot AI generated text than it is to spot SEO directed content written by humans.

Thread Thread
 
coderamrin profile image
Amrin

πŸ˜‚πŸ˜‚ too good

Collapse
 
digvijay25182316 profile image
Digvijay25182316 • Edited

useActionState : For Form Mutations.
useEffect : lifecycle hook to control components state

Collapse
 
random_ti profile image
Random

πŸ™Œ

Collapse
 
webjose profile image
JosΓ© Pablo RamΓ­rez Vargas

So React keeps on piling up things on top of things. Poor React devs, having to keep up with this hot mess.

Collapse
 
random_ti profile image
Random

πŸ˜…πŸ˜…

Collapse
 
nospipi profile image
Vaggelis Magonezos

I cant get the message from useActionState to return the server action value, see example

server file :

"use server";
const someServerAction = async (prevState,formData) => {
  try {
    return "success";
  } catch (error) {
    return "error";
  }
};
Enter fullscreen mode Exit fullscreen mode

client file :

"use client"
const SomeClientComponent = () => {
  const [message, formAction, isPending] = useActionState(
    someServerAction,
    "initial"
  );

  console.log(message)

// i get the initial string as defined in the hook, but after form submission, the message becomes undefined where it should log "success" - what am i missing ??

  return (
    <form
      action={formAction}
      className="space-y-6"
    >

..rest is irrelevant
Enter fullscreen mode Exit fullscreen mode
Collapse
 
shaogat_alam_1e055e90254d profile image
Shaogat Alam

Interesting topic! Everything is explained articulately and clearly. For your project, consider checking out this free npm package: select-paginated.

Collapse
 
devluc profile image
Devluc

Great article Taqui. I enjoyed learning from it

Collapse
 
random_ti profile image
Random

Thanks Lucian πŸ‘

Collapse
 
sanzuke profile image
Muhammad Hasanudin

nice share πŸ˜ƒπŸ‘πŸ»

Collapse
 
random_ti profile image
Random

Happy to hear that you liked it 😊

Collapse
 
gopalhb profile image
gopal keshri hb
get your code
Enter fullscreen mode Exit fullscreen mode
Collapse
 
random_ti profile image
Random

πŸ˜…

Collapse
 
syedmuhammadaliraza profile image
Syed Muhammad Ali Raza

Thanks for Sharing

Collapse
 
random_ti profile image
Random

Happy to hear you liked it πŸ₯°

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
phoenixdahdev profile image
Phoenix

what is the major difference between this and useTransition????

Collapse
 
justynclark profile image
Justyn Clark

Funny how everything is trying to catch up to Remix.run / React Router 7.

This is old news πŸ˜‚