2016-06-24 57 views
5

的功能傳遞子組件作爲參數傳遞給父組件的功能,並試圖使不起作用是否有可能通過一個陣營組件作爲參數在一個容器組件

//React Container Component 
//Import Both Views and Render based on preference 
import PosterView from "./PosterView" 
import ListView from "./ListViewCard" 

... 

renderCardsBasedOnType(cardType){ 
    if(cardType === "poster"){ 
    return this.renderCards(PosterView) 
    }else{ 
    return this.renderCards(ListViewCard) 
    } 
} 
renderCards(component){ 
    let cards = this.props.list.map(function(cardData){ 
    return <component data={cardData}/> 
    }) 
    return cards 
} 
render(){ 
    let cards = this.renderCardsBasedOnType("poster") 
    return <div>{cards}</div> 
} 
...... 

回答

9

嘗試Component代替component之間。 React需要一個用於jsx標記的upperCase:

renderCards(Component){ 
    let cards = this.props.list.map(function(cardData){ 
    return <Component data={cardData}/> 
    }) 
    return cards 
} 
+1

哇。感謝你!我只是浪費了兩個小時試圖弄清楚爲什麼我的組件沒有渲染。這樣做的工作。太糟糕了,以下推薦的JS風格在這裏有微妙的副作用... – Form

相關問題