Hone logo
Hone
Problems

Dynamic Component Loading and Rendering in React

Runtime integration allows you to load and render components dynamically, based on configuration or user input, without needing to rebuild your application. This is incredibly useful for plugin systems, A/B testing, or scenarios where you want to extend your application's functionality without modifying the core codebase. This challenge asks you to build a React component that can load and render components specified by their names at runtime.

Problem Description

You need to create a DynamicComponentLoader component that accepts a componentName prop. This componentName will correspond to a component registered within a componentMap. The DynamicComponentLoader should then dynamically import and render the component specified by componentName from the componentMap.

Key Requirements:

  • componentMap: A prop that is an object where keys are strings representing component names and values are React components.
  • componentName: A prop (string) specifying the name of the component to load and render.
  • Dynamic Import: Use dynamic imports (import()) to load the component at runtime.
  • Error Handling: If the componentName is not found in the componentMap, display an error message "Component not found".
  • Props Passing: The DynamicComponentLoader should accept a props prop (an object) which will be passed as props to the dynamically loaded component.

Expected Behavior:

The DynamicComponentLoader should render the component specified by componentName if it exists in the componentMap, passing the provided props. If the component is not found, it should render the error message.

Edge Cases to Consider:

  • What happens if the dynamic import fails? (Consider a fallback mechanism or error display). For simplicity, assume the dynamic import will always succeed for components defined in the componentMap.
  • What if componentName is an empty string or null? (Handle gracefully, perhaps by rendering a default component or an error message).
  • What if props is null or undefined? (Pass an empty object as props in this case).

Examples

Example 1:

Input:
componentMap: {
  "ComponentA": () => <h1>Component A</h1>,
  "ComponentB": () => <p>Component B</p>
}
componentName: "ComponentA"
props: { message: "Hello from props!" }

Output:
<h1>Component A</h1>
Explanation: ComponentA is found in the componentMap and rendered with the provided props.

Example 2:

Input:
componentMap: {
  "ComponentA": () => <h1>Component A</h1>,
  "ComponentB": () => <p>Component B</p>
}
componentName: "ComponentC"
props: { data: 123 }

Output:
Component not found
Explanation: ComponentC is not found in the componentMap, so the error message is displayed.

Example 3:

Input:
componentMap: {
  "ComponentA": () => <h1>Component A</h1>,
}
componentName: ""
props: null

Output:
Component not found
Explanation: componentName is an empty string, which is treated as not found. props is null, so an empty object is used.

Constraints

  • The componentMap will always contain valid React components as values.
  • componentName will be a string.
  • props will be an object or null/undefined.
  • The solution must be written in TypeScript.
  • The solution should be reasonably performant; avoid unnecessary re-renders.

Notes

  • Consider using React's Suspense for a more robust loading experience, although this is not strictly required for this challenge.
  • Think about how to handle potential errors during the dynamic import process (although, as mentioned, assume success for components in the map).
  • Focus on the core logic of dynamically loading and rendering components based on a string name. Styling and complex layouts are not the focus of this challenge.
  • The componentMap is passed as a prop, so you don't need to define it within the component itself.
Loading editor...
typescript