Hone logo
Hone
Problems

Building a Suspenseful Reveal Component in React with TypeScript

This challenge focuses on creating a reusable React component that reveals content gradually over time, simulating a suspenseful reveal effect. This is useful for displaying data that loads asynchronously, creating engaging animations, or building interactive storytelling experiences. You'll be leveraging React's state management and setInterval to control the reveal process.

Problem Description

You are tasked with building a SuspenseReveal component that takes a string as input and reveals it character by character at a specified interval. The component should initially display an empty string and then progressively reveal the input string, one character at a time, until the entire string is visible. The reveal speed should be configurable.

Key Requirements:

  • Input: The component must accept a text prop (string) and an interval prop (number, in milliseconds).
  • State Management: Use React's state to manage the currently revealed portion of the text.
  • Reveal Logic: Implement a timer using setInterval that reveals one character of the input text at a time.
  • Clear Display: The component should display the currently revealed text.
  • Cleanup: Ensure the setInterval is cleared when the component unmounts to prevent memory leaks.
  • Error Handling: Handle the case where the input text is empty or interval is invalid (e.g., less than or equal to 0).

Expected Behavior:

  • Initially, the component should display an empty string.
  • After the specified interval, one character of the text should be revealed.
  • This process should continue until the entire text is displayed.
  • If the text is empty, the component should display an empty string indefinitely.
  • If the interval is invalid, the component should display an error message (e.g., "Invalid interval").

Edge Cases to Consider:

  • Empty input string.
  • Invalid interval value (0 or negative).
  • Component unmounting before the reveal is complete.
  • Very long input strings.

Examples

Example 1:

Input: text="Hello", interval=500
Output: (After 500ms) "H" (After 1000ms) "He" (After 1500ms) "Hel" (After 2000ms) "Hello"
Explanation: The text "Hello" is revealed character by character every 500 milliseconds.

Example 2:

Input: text="", interval=1000
Output: ""
Explanation: An empty string is displayed indefinitely because the input text is empty.

Example 3:

Input: text="World", interval=-100
Output: "Invalid interval"
Explanation: An error message is displayed because the interval is invalid.

Constraints

  • The text prop must be a string.
  • The interval prop must be a number greater than 0.
  • The component should be performant and avoid unnecessary re-renders. Use useCallback where appropriate to prevent unnecessary re-creation of functions.
  • The component should be reusable and easily integrated into other React applications.

Notes

  • Consider using the useState hook to manage the revealed text.
  • setInterval is crucial for the timing mechanism. Remember to clear the interval using clearInterval when the component unmounts.
  • Think about how to handle the edge cases gracefully.
  • Focus on creating a clean, readable, and maintainable component.
  • You can use CSS to style the revealed text as desired (e.g., adding a fade-in effect). Styling is not the primary focus of this challenge, but feel free to add it if you wish.
Loading editor...
typescript