#how to "subscribe" to a server-actions with useFormState in a different component?

5 messages · Page 1 of 1 (latest)

hybrid hawk
#

Hey, I want to get the response from a server action into another client component, is there a way to subscribe to this server action, or would I need to set some context and update the context whenever the form returnes a different state?

This is what I'd like to do:

Submit the form here:

  const t = useTranslations('cart');
  const [state, action] = useFormState(validateCart, {
    message: '',
  });

  return (
    <form action={action}>
      <Button
        className="w-full"
        type="submit"
      >
        {t('next_cart_summary_validate_order')}
      </Button>
    </form>
  );
}

Retrieve the data of the not valid cart here:


export default function CartModified() {
  const [state] = useFormState(validateCart, {
    message: '',
  });

  useEffect(() => {
    console.log('CartModified useEffect', state);
  }, [state]);

  return (
    <div className="flex flex-col">
      <Text>Cart modifications:</Text>
      {state?.message}
    </div>
  );
}

Is this possible or am I thinking too far out of the box?

somber troutBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

maiden urchin
#

@hybrid hawk use context, or url params

hybrid hawk
#

Thanks!

maiden urchin