2016-12-27 44 views
0

有人可以幫助我渲染具有變量名稱的反應組件嗎?例如,我有一個組件導入3個其他組件,我想循環遍歷組件數組並渲染它們。很粗糙的僞代碼:如何渲染React.js中的變量組件

import component1 from '...'; 
import component2 from '...'; 
import component3 from '...'; 

const component_list = ['component1', 'component2', 'component3'] 

renderComponents() { 
    return this.component_list.map((component) { 
     <div> 
     <{component} /> 
     </div> 
    }); 
} 
+0

我會說你已經是他們的專家爲大括號。 GJ – DirtyRedz

回答

1

更新與組件類(而不是字符串)組件列表,並注意自定義組件must be capitalised

import component1 from '...'; 
import component2 from '...'; 
import component3 from '...'; 

const component_list = [component1, component2, component3]; 

renderComponents() { 
    return this.component_list.map((Component) => { 
     <div> 
     <Component /> 
     </div> 
    }); 
} 
0
import component1 from '...'; 
import component2 from '...'; 
import component3 from '...'; 

const component_list = [component1, component2, component3] 

renderComponents() { 
    return component_list.map((component) => { 
     <div> 
     <component /> 
     </div> 
    }); 
}