Hone logo
Hone
Problems

Vue Lifecycle Hooks Explorer

This challenge focuses on understanding and implementing Vue's component lifecycle hooks in a TypeScript environment. Mastering these hooks is crucial for managing component state, performing side effects, and orchestrating complex component behaviors effectively. You will build a small Vue component that demonstrates the invocation of various lifecycle hooks during its creation and destruction.

Problem Description

Your task is to create a Vue component using TypeScript that showcases the behavior of several key lifecycle hooks. You need to log messages to the console indicating when each hook is called, both during the component's initial mounting and when it is unmounted.

Key Requirements:

  1. Component Creation: Create a Vue component named LifecycleExplorer.
  2. Hook Implementation: Implement the following lifecycle hooks within the LifecycleExplorer component:
    • beforeCreate
    • created
    • beforeMount
    • mounted
    • beforeUnmount
    • unmounted
  3. Console Logging: Inside each implemented hook, log a descriptive message to the console indicating which hook was just executed. For example, beforeCreate hook called.
  4. Component Rendering: The component should have a simple template that displays a piece of data, which can be initialized in the created hook.
  5. Conditional Rendering (for unmount demonstration): Implement a mechanism in a parent component to conditionally render the LifecycleExplorer component. This will allow you to observe the beforeUnmount and unmounted hooks being called.

Expected Behavior:

When the LifecycleExplorer component is first rendered, the console output should reflect the following order of hook execution:

  1. beforeCreate hook called
  2. created hook called
  3. beforeMount hook called
  4. mounted hook called

When the LifecycleExplorer component is removed from the DOM (e.g., due to conditional rendering), the console output should reflect the following order:

  1. beforeUnmount hook called
  2. unmounted hook called

Examples

Example 1: Initial Component Mount

Assume a parent component that simply renders <LifecycleExplorer />.

  • Input: Initial rendering of the LifecycleExplorer component.
  • Output (Console Logs):
    beforeCreate hook called
    created hook called
    beforeMount hook called
    mounted hook called
    
  • Explanation: The component is instantiated (beforeCreate, created), its DOM is prepared (beforeMount), and it's attached to the DOM (mounted).

Example 2: Component Unmount

Assume a parent component with a button that toggles the visibility of <LifecycleExplorer /> using v-if. When the button is clicked to hide the LifecycleExplorer, and it was previously visible.

  • Input: The LifecycleExplorer component is removed from the DOM.
  • Output (Console Logs):
    beforeUnmount hook called
    unmounted hook called
    
  • Explanation: Before the component is fully removed, its cleanup logic is triggered (beforeUnmount, unmounted).

Constraints

  • Your solution must use Vue 3.x with TypeScript.
  • The component template can be a simple HTML structure (e.g., a div with a p tag).
  • All required lifecycle hooks listed must be implemented and produce console logs.
  • The parent component should be a simple Vue component demonstrating the v-if mechanism for unmounting.
  • No external Vue plugins or libraries beyond the core Vue 3 TypeScript setup are permitted.

Notes

  • Consider where you would typically initialize data or fetch information that doesn't require DOM access. Which hook is most appropriate?
  • Think about what kind of cleanup operations you might perform in beforeUnmount or unmounted.
  • The order of execution of these hooks is critical and fundamental to Vue's reactivity system.
  • Ensure your TypeScript setup correctly integrates with Vue's SFC (Single File Component) syntax.
Loading editor...
typescript