how to "subscribe" to a server-actions with useFormState in a different component?
Answered
Wesley Janse posted this in #help-forum
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:
Retrieve the data of the not valid cart here:
Is this possible or am I thinking too far out of the box?
This is what I'd like to do:
Submit the form here:
export default function CartSubmit({ entries }: Props) {
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?
3 Replies
@Wesley Janse use context, or url params
Answer
Thanks!
@Wesley Janse Thanks!
mark a solution if it helped :D