Home > other >  Call react component with string not by its name
Call react component with string not by its name

Time:06-08

How can i call the react function component with string Ex i have function component name function1 <function1 {...props} /> What if i have string name and want call it linke function component <name {...props />

CodePudding user response:

may be doing something in the likes of :

import React from "react";
import UserA from "./userA";
import UserB from "./userB";

const components = {
  usera: UserA,
  userb: UserB
};

function DynamicComponent(props) {
  const SelectUser = components[props.user];
  return <SelectUser />;
}

export default DynamicComponent;
  • Related