PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

React · Theory

String to Component in React

← All stacks

Theory

78/94

String to Component in React

Map a string to a component: const MAP = { hello: Hello, bye: Bye }; const Comp = MAP[kind] || Fallback; return <Comp />. Don’t eval(). Don’t dangerouslySetInnerHTML untrusted HTML. That’s how widget builders stay safe.

String to Component in React — kind=bye → Bye. A dictionary of components is the safe pattern.

Diagram
Parent
    │ props (read-only)
    ▼
  Child → UI
Exam tip

String → component map. Never eval.

Example

// String to component
const MAP = {
  hello: function Hello() { return <p>Hi</p>; },
  bye: function Bye() { return <p>Bye</p>; },
};

export default function Switch({ kind }) {
  const Comp = MAP[kind] || MAP.hello;
  return <Comp />;
}

String to Component in React — kind=bye → Bye. A dictionary of components is the safe pattern.

Short notes

  • DefLookup dictionary.
  • RuleFallback component.
  • RememberNo eval / random HTML dump.

Questions

1

Explain String to Component as if you are teaching a junior — definition, then one tiny UI.

2

Write 5–10 lines that use String to Component and say what the UI does.

3

What mistake do freshers make with String to Component?

Previous← Constructor in Function ComponentsNextStyling React with CSS →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.