I'm trying to create multiple React components dynamically from a string entered by the user. Each letter is supposed to be rendered as a separate component.
My plan was to create an array from the string and map over it returning each component in a new array.
My main issue is to convert the letter (string) into the component's name.
import React from 'react'
import './App.css'
import A from './ComponentA'
import B from './ComponentB'
import C from './ComponentC'
const userInput = "ABC"
const userInputArray = userInput.split("")
const components = userInputArray.map((comp, index) => {
return (
React.createElement(eval(comp), { key: [index]})
)
})
function App() {
return (
<>
{ components }
</>
)
}
export default App
I thought I found a solution using 'eval' but it fails when minified in production.
(Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: number.)
switchstatement that will return the needed component for each letter.