This can be done by assigning not a jsx but a reference to the component, then use jsx in a loop using the component from the variable. Check out my code changes.
render(){ let items = [{title:'hello'}, {title:'world'}]; let C = null; //null is more accurate for object variable switch (id) { case 1: C = A; //it is component reference, C must be from upper letter break; case 2: C = B; //it is component reference break; default: C = A; //good to have default for wrong ids } return( items.map((item, index)=>{ return( <span> <C {...item} /> //render component with props </span>11 ) }) ) }
The most important things:
1. C=A; we set the value of the variable C for the target component
2. <C {...item} /> all properties of the object's object will be set in the child component.
3. It can be used in a standard way, for example: <C title={item.title} />
Some working examples: https://jsfiddle.net/maciejsikora/jtt91wL3/3/
source share