Frontend School

Build a Resilient Toast Notification System

Machine Coding
easy

Build a Resilient Toast Notification System

Easy
React
Frontend
State Management
UI Components
Notifications
Hooks

Overview

In modern web applications, providing timely and non-intrusive feedback to users is crucial for a smooth user experience. Toast notifications are a common pattern for displaying transient messages (e.g., "Action successful!", "Item added to cart") without interrupting the user's workflow. This problem challenges you to build a foundational toast notification system, focusing on state management and component composition.

What you need to do / Task

Your goal is to implement a reusable and robust toast notification system in React. This involves creating a ToastProvider component, a useToast custom hook, and a Toast display component.

  1. Implement ToastProvider: This component should manage the state of all active toasts. It will wrap your application (or a part of it) and provide the showToast functionality via React Context.
  2. Implement useToast hook: This custom hook will allow any component within the ToastProvider's scope to easily trigger new toast notifications.
  3. Implement Toast component: This component will be responsible for rendering an individual toast message, including a dismiss button.
  4. Add Toast Functionality:
    • When showToast is called, a new toast should appear on the screen.
    • Each toast should automatically dismiss itself after a short duration (e.g., 3-5 seconds).
    • Each toast should have a visible 'X' or 'Dismiss' button, allowing users to manually dismiss it before the auto-dismissal.
    • Multiple toasts should be able to appear concurrently, stacking neatly.

UI/UX expectations

  • Toasts should appear consistently in the top-right corner of the viewport.
  • Each toast should display its message and a clickable dismiss button (e.g., 'X').
  • When multiple toasts are active, they should stack vertically, with newer toasts appearing above older ones (or vice-versa, as long as it's consistent).
  • Basic styling for toasts (background, text color, padding) has been provided in styles.css.

Acceptance criteria

  • A button click in App successfully triggers a new toast notification.
  • New toasts appear in the top-right corner.
  • Multiple toasts stack correctly without overlapping.
  • Each toast displays its provided message.
  • Each toast includes a functional dismiss button that removes it from the display.
  • Toasts automatically disappear after a fixed duration (e.g., 3000ms).
  • The system should not error when rapidly adding or dismissing toasts.

Constraints

Constraints & assumptions

  • You must use React functional components and hooks.
  • Do not use any external third-party libraries specifically for toast notifications (e.g., react-toastify). The goal is to implement the core logic yourself.
  • The duration for auto-dismissal can be a hardcoded constant (e.g., 3000ms).
  • Focus on the core logic and state management; advanced animations are not required for this problem.

Out of scope

  • Different types of toasts (e.g., success, error, warning) with distinct styling.
  • Complex entry/exit animations beyond basic display: none or opacity transitions.
  • Custom positioning options for the toast container.
  • Accessibility features beyond basic button functionality (e.g., ARIA live regions).

What interviewers evaluate

  • State Management: How effectively you manage the list of active toasts.
  • React Context & Hooks: Correct usage of createContext, useContext, and useState/useEffect/useCallback.
  • Component Composition: How well you structure ToastProvider and Toast components.
  • Clean Code: Readability, organization, and maintainability of your code.

Loading editor...