简体   繁体   English

如何使用带有React / Redux的组件将2个模态合并为1个?

[英]How can I merge 2 modals into 1 using components with React / Redux?

I have a signup modal that looks like: 我有一个注册模式,如下所示:

在此处输入图片说明

and a login modal that looks like: 和一个登录模式,如下所示: 在此处输入图片说明

Right now, I have them as 2 separate React Components triggered from my NavBar as follows: 现在,我将它们作为从NavBar触发的2个独立的React组件,如下所示:

  <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>

I'd like to combine them into one modal component (potentially called AuthModal ) and that'll load the appropriate component that is selected. 我想将它们组合为一个模态组件(可能称为AuthModal ),这将加载所选的适当组件。

I am using react-bootstrap if that matters. 如果这很重要,我正在使用react-bootstrap I'm new to React, so if something is unclear, please let me know and I'll clarify. 我是React的新手,所以如果不清楚,请告诉我,我会澄清。

Thanks. 谢谢。

Gonna give you an idea. 要给你一个主意。

You can setup it this way. 您可以通过这种方式进行设置。

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

So you call you modal like <Modal /> and if you need to be the auth one you do like <Modal auth={true} /> 因此,您称自己为<Modal />类的<Modal /> ,如果您需要成为身份验证者,则可以像<Modal auth={true} />一样进行<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>

Hope that can help ;) 希望能有所帮助;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM