Hone logo
Hone
Problems

Dynamic Component Rendering with Props in Vue (TypeScript)

This challenge focuses on building a Vue component that dynamically renders other components based on a prop. Dynamic component rendering is a powerful technique for creating flexible and reusable UI elements, allowing you to switch between different components at runtime. This is particularly useful for scenarios like displaying different content types or creating configurable dashboards.

Problem Description

You are tasked with creating a DynamicComponentWrapper component in Vue. This component will accept a componentName prop, which will be a string representing the name of a registered component. It will also accept a props prop, which will be an object containing key-value pairs to be passed as props to the dynamically rendered component. The DynamicComponentWrapper should render the component specified by componentName, passing the provided props to it.

Key Requirements:

  • The DynamicComponentWrapper must accept componentName and props as props.
  • The componentName prop should be used to dynamically determine which component to render.
  • The props prop should be passed as props to the dynamically rendered component.
  • The component should handle cases where the specified componentName is not registered. In such cases, it should render a placeholder message indicating that the component was not found.
  • The solution must be written in TypeScript.

Expected Behavior:

When the DynamicComponentWrapper receives a valid componentName and props, it should render the corresponding component with the provided props. If the componentName is invalid, it should display a message like "Component not found: [componentName]".

Edge Cases to Consider:

  • What happens if componentName is an empty string or null/undefined? Should it render a placeholder or throw an error? (Placeholder is preferred).
  • What happens if props is null/undefined? Should it pass empty props or throw an error? (Passing empty props is preferred).
  • How should the component handle potential errors within the dynamically rendered component? (For simplicity, error handling within the dynamic component is outside the scope of this challenge).
  • Consider the type safety of the props object. While full type checking is complex, aim for reasonable type safety.

Examples

Example 1:

Input:
componentName: "MyButton"
props: {
  label: "Click Me",
  color: "blue"
}

Output:
A MyButton component is rendered with the label "Click Me" and color "blue".
Explanation: The DynamicComponentWrapper renders the MyButton component and passes the specified props.

Example 2:

Input:
componentName: "NonExistentComponent"
props: {
  message: "Hello"
}

Output:
A message "Component not found: NonExistentComponent" is displayed.
Explanation: The DynamicComponentWrapper detects that "NonExistentComponent" is not a registered component and displays the error message.

Example 3:

Input:
componentName: "MyInput"
props: null

Output:
A MyInput component is rendered with no props.
Explanation: The DynamicComponentWrapper renders the MyInput component and passes an empty object as props.

Constraints

  • The solution must be a valid Vue component written in TypeScript.
  • The componentName prop must be a string.
  • The props prop must be an object.
  • The solution should be reasonably performant. Avoid unnecessary re-renders.
  • Assume that the components referenced in componentName are already registered globally within the Vue application.

Notes

  • You can use the defineAsyncComponent function for more complex scenarios involving asynchronous component loading, but for this challenge, assume the components are readily available.
  • Consider using the h function (createElement) from Vue to create the dynamic component.
  • Think about how to handle the type safety of the props object. While perfect type safety is difficult to achieve without more context about the components being rendered, strive for reasonable type checking. Using generics can be helpful.
  • Focus on the core logic of dynamic component rendering and prop passing. Styling and complex component interactions are not required.
Loading editor...
typescript