在react中单击时呈现新组件

IT技术 javascript reactjs
2021-05-02 21:02:26

我正在练习react并尝试通过单击按钮来呈现新组件。这里的第一页是电子邮件,我要呈现的组件包含密码页面。

  class App extends React.Component {
 passwordpage(){
    return(
      <form>
        <div className="mainapp">
          <h2> Password</h2>
          <input type='Password' className="inputpassword" placeholder='Enter Password' required/>
          <div>
            <button type="submit" className="loginbutton">Next</button>
          </div>
        </div>
      </form>
    );
  };

  render() {
    return (
      <form>
        <div className="mainapp">
          <h2>Email  Id</h2>
          <input type='email' ref='enteremail'className="inputemail" placeholder='Enter Username' required/>
          <div>
            <button type="submit" onClick={this.props.passwordpage} className="loginbutton">Next</button>
          </div>
        </div>
      </form>

    );
  }
}

 ReactDOM.render(<App/>,document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react-dom.min.js"></script>
<div id="app"></div>

3个回答

最简单的方法是在您的 中准备好密码页面render(),并根据组件状态显示/隐藏它,该状态由onClick处理程序改变,类似以下内容:

showPasswordPage() {
  this.setState({showPassword: true });
}

render() {
  const passwordPage = (<form>
    <div className="mainapp">
      <h2> Password</h2>
      <input type='Password' className="inputpassword" placeholder='Enter Password' required/>
      <div>
        <button type="submit" className="loginbutton">Next</button>
      </div>
    </div>
  </form>);

  const mainForm = (<form>
    <div className="mainapp">
      <h2>Email  Id</h2>
      <input type='email' ref='enteremail'className="inputemail" placeholder='Enter Username' required/>
      <div>
        <button type="submit" onClick={this.showPasswordPage} className="loginbutton">Next</button>
      </div>
    </div>
  </form>);

  return { this.state.showPassword ? passwordPage : mainForm };

我通常会保留一些状态变量并根据用户操作更改它们。

因此,在您的情况下,我已存储当前活动页面,例如usernamePage,当用户单击下一步时,我会在您的情况下显示另一个页面passwordPage

class App extends React.Component {
  
  constructor(props) {
    super(props);

    this.state = {
      isPasswordPage : false,
      isUsernamePage : true,
      username       : "",
      password       : ""
    };

    this.enablePasswordPage  = this.enablePasswordPage.bind(this);
  }

  enablePasswordPage() {
    this.setState({
      isPasswordPage : true,
      isUsernamePage : false
    });
  }
  
  passwordpage(){
    return(
      <form>
        <div className="mainapp">
          <h2> Password</h2>
          <input type='Password' className="inputpassword" placeholder='Enter Password' required/>
          <div>
            <button type="submit" className="loginbutton">Next</button>
          </div>
        </div>
      </form>
    );
  };

  render() {
    var usernameComp = (
      <form>
        <div className="mainapp">
          <h2>Email  Id</h2>
          <input type='email' ref='enteremail'className="inputemail" placeholder='Enter Username' required/>
          <div>
            <button onClick={this.enablePasswordPage} className="loginbutton">Next</button>
          </div>
        </div>
      </form>
    );
    
    return (
      <div>
        { this.state.isUsernamePage ? usernameComp : null }
        { this.state.isPasswordPage ? this.passwordpage() : null }
      </div>
    );
  }
}

ReactDOM.render(<App/>,document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react-dom.min.js"></script>
<div id="app"></div>

您需要做的是利用react-router然后为您要显示的每个页面创建单独的组件。这是实现您想做的事情的最佳方式。

你的组件看起来像

class Home extends React.Component {
    render() {
    return (
        <div>{this.props.children}</div>
    )
  }
}

class App extends React.Component {

  handleClick = (e) => {
     e.stopPropagation();
     browserHistory.push('/passwordPage');
  }
  render() {
    return (
      <form>
        <div className="mainapp">
          <h2>Email  Id</h2>
          <input type='email' ref='enteremail'className="inputemail" placeholder='Enter Username' required/>
          <div>
            <button className="loginbutton" onClick={this.handleClick}>Next</button>
          </div>
        </div>
      </form>

    );
  }
}

class PasswordPage extends React.Component {
    render() {
    return (
        <form>
        <div className="mainapp">
          <h2> Password</h2>
          <input type='Password' className="inputpassword" placeholder='Enter Password' required/>
          <div>
            <button type="submit" className="loginbutton">Next</button>
          </div>
        </div>
      </form>
    )
  }
}

ReactDOM.render(
   <Router history={browserHistory}>
   <Route path="/" component={Home}>
    <IndexRoute component={App} />
    <Route path="/passwordPage" component={PasswordPage} />
   </Route>
   </Router>
)

这里查看 react-router 文档