2016-10-04 28 views
0

我有一個註冊模式,看起來像:如何使用React/Redux組件將2個模塊合併爲1?

enter image description here

和登錄模式,看起來像: enter image description here

現在,我讓他們爲2個獨立的反應的組分從我的NavBar作爲觸發如下:

<Nav pullRight className="navright"> 
    <NavItem eventKey={3} href="#">Stuff</NavItem> 
    <NavItem eventKey={4} href="#">Blog</NavItem> 
    <NavItem href="#" className={this.state.signedIn ? '' : 'hidden'}>{this.state.name}</NavItem> 
    <NavItem eventKey={2} href="#" className={this.state.signedIn ? 'hidden' : ''}><SignupModal/></NavItem> 
    <NavItem eventKey={1} href="#" className={this.state.signedIn ? 'hidden' : ''}><LoginModal/></NavItem> 
    </Nav> 

我想將它們組合成一個模態分量(可能稱爲AuthModal)和塔將加載所選的適當組件。

如果有問題,我正在使用react-bootstrap。我是React的新手,所以如果有什麼不清楚的地方,請讓我知道,我會澄清。

謝謝。

+0

您需要的內容結合SignupModal和LoginModal組成一個名爲AuthModal的新組件。從渲染功能開始,並以功能的方式工作。它與任何其他反應/ DOM工作沒有區別。 –

回答

2

會給你一個想法。

你可以這樣設置它。

const Modal = ({ auth }) => { 
    if (auth) { 
    return { 
     <div>all your stuff here</div> 
    } 
    } 
    return { 
    <div>all your other stuff here</div> 
    } 
} 

所以,你打電話給你莫代爾像<Modal />,如果你需要將已授權一個你不喜歡<Modal auth={true} />

<Nav pullRight className="navright"> 
    <NavItem eventKey={3} href="#">Stuff</NavItem> 
    <NavItem eventKey={4} href="#">Blog</NavItem> 
    <NavItem href="#" className={this.state.signedIn ? '' : 'hidden'}>{this.state.name}</NavItem> 
    <NavItem eventKey={2} href="#" className={this.state.signedIn ? <Modal /> : <Modal auth={true} />}></NavItem> 
    </Nav> 

希望能幫助;)

+0

這太棒了!謝謝。我怎樣才能傳遞一個字符串而不是'true'? – Shamoon

+0

您可以傳遞一個字符串'auth = {'myString'}',並在組件內部您可以 'if(auth ==='myString')' – EQuimper

+1

我編輯我的答案,向您展示如何使用您的電話東東。 – EQuimper