I want to create a list of component using strings. Searching on Google/StackOverflow, this seems to be easy:
import Light from 'light.jsx';
[...]
getComponent()
{
let DynamicComponent = "Light";
return <DynamicComponent />;
}
Unfortunally, this approach doesn't work. I get this error non console:
Warning: <Light /> is using uppercase HTML. Always use lowercase HTML tags in React. Warning: The tag <Light> is unrecognized in this browser. If you meant to render a React component, start its name with an uppercase letter.
I've tried many combinations like:
return React.createElement(DynamicComponent, {}) };
return React.createElement("Light", {}) };
return <div> { React.createElement(DynamicComponent, {}) }</div>;
They all returns the same error.
Obviously if I use:
return <Light />
it works.
let map = {"Light": Light, "Fan": Fan}then use this:let DynamicComponent = map["Light"], and the you will be able to render it like this:<DynamicComponent />, let me know if it fails to solve your issue or you need any other help.import Light ....;, import Fan ....etc?import * as Dynamic from 'allimport.js';then:let DynamicComponent = "Dynamic.Light"; return <DynamicComponent />then in the allimport.jsimport Light from './light.jsx'; import Fan from './fan.jsx';