calling component with dynamic name in react js

PHOTO EMBED

Mon Jul 29 2024 11:43:47 GMT+0000 (Coordinated Universal Time)

Saved by @codejck #react.js #component

import React from 'react';

const ComponentA = () => <h1>Component A</h1>;
const ComponentB = () => <h2>Component B</h2>;

const componentMap = {
  a: ComponentA,
  b: ComponentB,
};

const MyComponent = ({ type }) => {
  const ComponentToRender = componentMap[type];

  return <ComponentToRender />;
};

export default MyComponent;
content_copyCOPY