2017-03-07 122 views
0

我有一個HeadingComponent其示出了H 2標籤像這樣內頁標題:通道具反應

<div id="id1"> 
     <h2 className="class1">{headingText}</h2> 
</div> 

HeadingComponent是具有其他組件和嵌入的div父DIV中。 ComponentThatDecidesHeading1,ComponentThatDecidesHeading2,ComponentThatDecidesHeading3是將決定應該是什麼{headingText}即

<div id="layoutContentArea"> 
       <HeadingComponent headingText={headingText}/> 
       <div or some wrapper component> 
        <ComponentThatDecidesHeading1/> 
        OR 
        <ComponentThatDecidesHeading2/> 
        OR 
        <ComponentThatDecidesHeading3/> 
        </div> 
      </div> 

所以,如果ComponentThatDecidesHeading1呈現,headingText = '標題1',如果ComponentThatDecidesHeading2被渲染的成分,headingText ='標題2'等。

有沒有辦法把一個「如果」的條件或東西,檢查哪個組件被呈現,並根據顯示相應的headingText? 或 將headingText從,並傳入。

我檢查了ReactJS Two components communicating,Pass props to parent component in React.js,但沒有得到我的答案。

任何想法?

+0

誰選擇渲染哪個標題?它是由用戶完成的嗎? – Dhiraj

+0

爲什麼不製作組件 - 「ComponentThatDecidesHeading」並根據需要傳遞道具以確定組件/標題? –

+0

@ Dhiraj-存在組件「ComponentThatDecidesHeading1」,「ComponentThatDecidesHeading2」等決定標題。正如我所提到的,如果當前呈現的組件是「ComponentThatDecidesHeading1」,那麼標題將是「標題1」,如此等等。 「HeadingComponent」是在其「h2」標籤內顯示標題的組件。 – abhi

回答

0

您可以在render()函數中創建一個函數,以在設置標題文本的同時有條件地渲染所需組件。我假設你有一個「ComponentThatDecidesHeading」組件,它從別處接收不同的道具。如果是這樣的話,像這樣的工作(EDITED回覆您的查詢):

// Elsewhere in your app: 
 

 
const propsForMyHeadingComponents = [ 
 
{ 
 
    id: 1, 
 
    headingText: 'HeadingText 1', 
 
    data: [thing1, thing2...] 
 
}, 
 
{ 
 
    id: 2, 
 
    headingText: 'HeadingText 2', 
 
    data: [otherThing1, otherThing2, ...] 
 
}, 
 
etc... 
 
]; 
 

 
// Meanwhile, in your parent component: 
 

 
grabDataAndHeadingText(){ 
 
    let headingText; 
 
    let component; 
 

 
    // Assuming the info you need to load into your components is passed in 
 
    // from a parent or from a store like Redux: 
 
    const { propsForMyHeadingComponents } = this.props; 
 

 
    // From there, you can iterate through your array to select the data for the component you want: 
 
    propsForMyHeadingComponents.forEach(thisHeader => { 
 
    if(condition-to-determine-which-component-to-load === thisHeader.id){ 
 
     headingText = thisHeader.headingText; 
 
     data = thisHeader.data; 
 
    }); 
 
    
 
    return { data, headingText }; 
 
} 
 

 
render(){ 
 

 
    const { data, headingText } = this.grabDataAndHeadingText(); 
 

 
    // Again, assuming "ComponentThatDecidesHeading" is the same component 
 
    // with changing props: 
 

 
    return(
 
    <div id="layoutContentArea"> 
 
     <HeadingComponent headingText={ headingText }/> 
 
     <div or some wrapper component> 
 
      <ComponentThatDecidesHeading data={ data } /> 
 
     </div> 
 
    <div> 
 
); 
 
}

你總是可以得到&集headingText和組件變量狀態,也是如此。

如果你真的有100個獨立的組件,每個組件都有自己的一組獨特的邏輯,等等,你可以將組件的名稱存儲在你的「propsForMyHeadingComponents」數組中。否則,只需使用上面列出的要用其呈現的道具加載組件即可。

+0

謝謝Nate。是的,我將會有很多決定標題的組件(可能是100個)。你能分享另一種方法嗎? – abhi

+0

@abhi請參閱上文。 –

+1

它工作。就像你說的,我所有「決定標題」的組件都是彼此不同的(有自己獨特的邏輯等)。所以,我稍微修改了一下你的方法。 常量compWithNextPage = [ { 當前頁: 'ABC', 下一頁: 'XYZ', headingText: 'headin1', 的BodyContent: }, { 當前頁:「DEF, 下一頁: 'GHI', headingText: 'heading2', 的BodyContent: }, ] – abhi