
Enhance React server actions with react-hot-toast! Show users loading, success, and error messages in real-time for a clean, user-friendly experience.
Server actions in React can be tricky to manage, especially when it comes to providing users with feedback on the status of their action. In this article, we’ll explore a clean and user-friendly approach to handling React server actions using react-hot-toast.
I was using traditional server actions with <form action=""> and struggling to provide good user feedback. Then I discovered this cool method, and I hope you’ll find it helpful too!
react-hot-toast is a lightweight, customizable, and beautiful toast notification library for React. It’s perfect for showing real-time feedback to users during async actions. Using its toast.promise() method, you can manage loading, success, and error states in one neat package.
Let’s create a simple newsletter subscription form. Users will enter their email and click the Subscribe button. Here’s how we’ll handle it:
Here’s the complete code implementation.
interface ActionResponse {
status: 'success' | 'error';
message: string;
data?: any;
}This ensures consistency and clarity when working with server responses.
Subscribe component, we’ll use react-hot-toast to handle feedback during the subscription process:"use client"
const handleSubscribe = () => {
const result = subscribeToNewsletterAction(values);
toast.promise(result, {
loading: 'Subscribing...',
success: (data: ActionResponse) => {
setLoading(false);
if (data.status === 'success') {
form.reset();
return data.message;
}
throw new Error(data.message);
},
error: (err: any) => err.toString(),
});
}Here’s what’s happening:
export const subscribeToNewsletterAction = async (payload: z.infer<typeof newsletterSchema>): Promise<ActionResponse> => {
const input = newsletterSchema.parse(payload);
try {
// subscribe
return {
message: 'Thank you for subscribing! You’ll now receive the latest updates straight to your inbox.',
status: 'success'
}
} catch (error) {
return {
message: 'Oops! Something went wrong while processing your request. Please try again later.',
status: 'error'
}
}
}Here:
ActionResponse return type for typesafeServer actions are essentially public-facing endpoints. Avoid returning any sensitive data in the response. If necessary, implement proper authentication and authorization checks before executing the code to ensure secure access.
toast.promise(result, {
loading: {
message: 'Working on it...',
icon: '⚡️',
style: {
background: '#2196F3',
color: 'white',
},
},
success: {
message: data => `${data.message} 🎉`,
duration: 4000,
icon: '🌟',
},
error: {
message: err => getErrorMessage(err),
duration: 5000,
icon: '💫',
},
});For further options and details, check out https://react-hot-toast.com/
By combining React Server Actions with react-hot-toast, we've created a robust, user-friendly system for handling asynchronous operations. This approach not only improves the user experience but also makes our code more maintainable and type-safe.