React Transition Tracking Component
This challenge focuses on building a reusable React component that tracks and displays the transition state of another component. This is useful for debugging, visualizing animations, or providing user feedback during transitions (e.g., showing a loading indicator while a component is transitioning). You'll create a TransitionTracker component that wraps another component and monitors its transition state, providing a visual representation of the state changes.
Problem Description
You need to build a TransitionTracker component in React using TypeScript. This component will accept a child component as a prop and track its transition state. The transition state will be one of the following: Entering, EnteringDone, Exiting, ExitingDone, or Finished. The TransitionTracker should display the current transition state as a simple text label. The TransitionTracker should also provide a callback function to the child component that the child can call to signal the completion of a transition phase.
Key Requirements:
childprop: Accepts a React element (the component to be tracked).onTransitionCompleteprop: Accepts a callback function. This function should be called by the child component when a transition phase is complete.- Transition States: The component must accurately track and display the following states:
Entering,EnteringDone,Exiting,ExitingDone, andFinished. - Initial State: The initial state should be
Entering. - State Transitions: The state transitions should occur in the following order:
Entering->EnteringDone->Exiting->ExitingDone->Finished. - Callback Integration: The
onTransitionCompletecallback should be invoked when the state transitions toEnteringDone,ExitingDone, andFinished. - Rendering: The component should render a simple
<div>displaying the current transition state.
Expected Behavior:
- When the
TransitionTrackermounts, it should immediately set its state toEntering. - The child component should be rendered within the
TransitionTracker. - The child component should call the
onTransitionCompletecallback at appropriate times during its transition phases. - The
TransitionTrackershould update its state based on the calls toonTransitionComplete. - The
TransitionTrackershould display the current transition state in a readable format.
Edge Cases to Consider:
- What happens if the child component doesn't call
onTransitionCompleteat all? The component should eventually reach theFinishedstate, but it might take some time. - What happens if the child component calls
onTransitionCompletemultiple times in a row? The state should only transition once per call. - What happens if the child component calls
onTransitionCompleteout of order? (e.g., callsExitingDonebeforeExiting). The component should still function correctly, but the state might not reflect the intended transition sequence.
Examples
Example 1:
Input:
<TransitionTracker
child={<MyComponent onTransitionComplete={() => console.log("MyComponent transition complete")}/>}
/>
Output:
Initially: "Entering"
After MyComponent calls onTransitionComplete: "EnteringDone"
Later: "Exiting"
Later: "ExitingDone"
Finally: "Finished"
Explanation: The TransitionTracker starts in Entering, transitions to EnteringDone when MyComponent calls onTransitionComplete, then to Exiting, ExitingDone, and finally Finished.
Example 2:
Input:
<TransitionTracker
child={<MyComponent onTransitionComplete={() => console.log("Exiting Complete")}/>}
/>
Output: Initially: "Entering" After MyComponent calls onTransitionComplete: "EnteringDone" Later: "Exiting" Later: "ExitingDone" Finally: "Finished"
Explanation: Similar to Example 1, but focuses on the `Exiting` phase.
**Example 3:** (Edge Case - Child doesn't call onTransitionComplete)
Input: <TransitionTracker child={<MyComponent />} />
Output:
Initially: "Entering"
After a delay (simulating the component eventually finishing): "EnteringDone"
Later: "Exiting"
Later: "ExitingDone"
Finally: "Finished"
Explanation: The component will eventually transition to Finished even if the child doesn't explicitly call onTransitionComplete, simulating a timeout or default behavior.
Constraints
- The component must be written in TypeScript.
- The component should be reusable and not tightly coupled to any specific child component.
- The component should be performant and avoid unnecessary re-renders.
- The
onTransitionCompletecallback should be invoked only once per transition phase. - The component should handle potential errors gracefully.
Notes
- Consider using the
useStatehook to manage the transition state. - Think about how to ensure that the state transitions occur in the correct order.
- You can use a simple text label to display the transition state. Styling is not required for this challenge.
- The
MyComponentin the examples is a placeholder; you don't need to implement it. Focus on theTransitionTrackercomponent. - The
onTransitionCompletecallback is crucial for triggering the state transitions. Make sure the child component can reliably call this callback.