简体   繁体   English

如果使用React js成功登录,则重定向到另一个HTML页面

[英]Redirect to another html page if the login is success with react js

I am new to React, I would like to know if it is possible with React js. 我是React的新手,我想知道React js是否有可能。 for example I have a login modal, and I tried to write a basic script and take de value of inputs with this code document.getElementById("email"); 例如,我有一个登录模式,我尝试编写一个基本脚本,并使用此代码document.getElementById("email");输入值document.getElementById("email"); but not working. 但不起作用。 please help me, that I can use? 请帮助我,我可以使用吗? or how do it ? 还是怎么做? please, oh and sorry for my english :D 请,哦,对不起,我的英语:D

Yes, it's possible. 是的,有可能。

  1. You do not need to use document.getElementById("email"); 您不需要使用document.getElementById(“ email”);。 Check how 检查方式
    work with inputs https://facebook.github.io/react/docs/forms.html 使用输入https://facebook.github.io/react/docs/forms.html
  2. I suggest to add React Router https://github.com/reactjs/react-router-tutorial it helps you to redirect user when it's necessary in correct and elegant way. 我建议添加React Router https://github.com/reactjs/react-router-tutorial,它可以帮助您在必要时以正确且优雅的方式重定向用户。

Example: 例:

export default class Login extends React.Component {
constructor(props) {
    super(props);

    this.state = {
      login: '',
      password: '',
    };
    this.onChangeInput = this.onChangeInput.bind(this);
    this.onLogin = this.onLogin.bind(this);
    this.onHandleLogin = this.onHandleLogin.bind(this);
  }

  onHandleLogin() {
    /* using react router you can do this */
    browserHistory.push('/todo');
  }
  onShowLoginError() {
    this.setState({
      showError: true,
    });
  }

  onChangeInput({ target: { value, name } }) {
    this.setState({
      [name]: value,
    });
  }

  onLogin(e) { 
    e.preventDefault();
    /* check here credentials */
    /* I do request to server */
    const init = {
      method: 'POST',
      body: JSON.stringify(this.state),
    };
    /* if login is correct call this.onHandleLogin() */
    sendRequest('login', init, () => {})
      .then(() => this.onHandleLogin())
  }

  render() {
    return (
      <div>
        <h1>Login page...</h1>
        <form>
          <div>
            <label htmlFor="login">User name:</label>
            <input
              type="text"
              name="login"
              value={this.state.login}
              onChange={this.onChangeInput}
            />
          </div>
          <div>
            <label htmlFor="password">Password:</label>
            <input
              type="text"
              name="password"
              value={this.state.password}
              onChange={this.onChangeInput}
            />
          </div>
          <button onClick={this.onLogin}>Log in</button>
        </form>
      </div>
    );
  }
}

The full example see here https://github.com/Aksana-Tsishchanka/react-routing/blob/master/src/components/Login.jsx 完整示例请参见https://github.com/Aksana-Tsishchanka/react-routing/blob/master/src/components/Login.jsx

I'm very new also but wanted to help. 我也很新,但是想帮忙。 Along my project in react i found a good solution for that with https://firebase.google.com/ authentication that let me also offer facebook, google and more login methods and recommend you to try it, also has a very good documentation. 在我的React项目中,我通过https://firebase.google.com/身份验证找到了一个很好的解决方案,它还提供了facebook,google和更多登录方法,并建议您尝试使用它,同时它也提供了很好的文档。

I think you should implement something like: 我认为您应该执行以下操作:

componentDidMount() {
    if (this.state.userLoggedin) {  // you should save in state your logged user
       // actions if user is logged in  
    } else {
       // actions if not user logged in
      }
}

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

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