登录 React Redux 应用程序后重定向到主页

mic*_*ael 5 reactjs redux-thunk react-redux

我正在使用 Create-React-App 学习 React Redux

我在存储、减速器、动作等的逻辑上遇到了问题。

我有一个简单的登录页面(省略了一些 JSX 以使其更易于阅读)

登录.js

import React, { Component } from "react";
import { connect } from "react-redux";
import * as actions from '../../actions';
import Logo from "../img/image-center.png";
import "./login.css";


class Login extends Component {
  constructor(props){
    super(props);
    this.state = {
      errorMsg : ""
    };
    this.loginClicked = this.loginClicked.bind(this);   
    console.log("loggedIn:", this.props.login);
  }

  loginClicked() {
    try {
      this.props.loginUser(this.refs.email.value, this.refs.password.value)
      .then(() => {
        console.log("thisprops", this.props);        
      });
    }
    catch(ex){
      this.state.errorMsg = "Unable to connect to server";
      console.log("error", ex);
    }
  }

  render() {
    return (

      <div className="login-bg">
        <div className="container signForm">
            <div className="col s12 l6 login-form">
              <p className="center-align">Login to Trade Portal</p>
              <form>
                <div className="row">
                  <div className="input-field">
                    <input id="email" type="email" className="validate" ref="email" />
                    <label htmlFor="email">Email</label>
                  </div>
                </div>
                <div className="row">
                  <div className="input-field">
                    <input id="password" type="password" className="validate" ref="password" />
                    <label htmlFor="password">Password</label>
                  </div>
                </div>
                <div className="row">
                  <button
                    className="btn waves-effect waves-light"
                    type="button"
                    name="action"
                    onClick={this.loginClicked}
                    >
                    Submit
                  </button>
                  <a href="/subcontractor" className="register">
                    Register here
                  </a>
                </div>
                <div className="row"><div style={{textAlign: "center", color:"red"}}>{this.props.login.message}</div></div>   

              </form>
            </div>
          </div>
        </div>
      </div>
    );
  }
}


function mapStateToProps({login}){
  return { login } ;
}

export default connect(mapStateToProps, actions)(Login);
Run Code Online (Sandbox Code Playgroud)

如您所见,LoginClick 函数调用了一个使用 connect 派生的道具 loginUser(实际上调用了我的 Action Login 方法)

动作\ index.js

import axios from "axios";
import { FETCH_USER, LOGIN_USER, LOGIN_FAILED } from "./types";


export const fetchUser = () => async dispatch => {
  const res = await axios.get("/api/Account/GetUser");
  dispatch({ type: FETCH_USER, payload: res.data });
};

export const loginUser = (username, password) => async dispatch => {
    try {
        const res = await axios.post("/api/Account/Login", {username: username, password: password, persistant: false });
        const message = res.data ? "" : "Incorrect username or password";
        dispatch({ type: LOGIN_USER, payload: { success: res.data, message: message } });
    }
    catch(ex){
        console.log("Login Failed:", ex);
        dispatch({ type: LOGIN_FAILED, payload: { success: false, message: "Unable to connect to authentication server" } });
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码调用我的服务器并登录用户,成功后在 loginReducer.js 中读取

import { LOGIN_USER, LOGIN_FAILED } from "../actions/types";

export default function(state = null, action) {
  console.log(action);
  switch (action.type) {
    case LOGIN_USER:      
      return { success: action.payload.success, message: action.payload.message };
      break;
    case LOGIN_FAILED:
      return { success: false, message: action.payload.message };
      break;
      default:
    return { success: false, message: ""};
  }
}
Run Code Online (Sandbox Code Playgroud)

现在我的问题是如何在成功登录主页时将用户重定向到何处以及如何重定向?“/家”

我希望我可以在 Login.js 的某个地方做到这一点,因为有一天我可能想要有 2 个登录路由(即,如果从标题登录,它可能不会重定向到主页,但是当从 Login.js 登录时,它应该登录到家)。因此我不认为这个动作应该放在 Action 或 Reducer 中。但我不确定如何在 Login.js 中绑定它,所以当登录成功时,重定向

May*_*kla 4

可能的方式-

\n\n

1-成功登录后从loginClicked方法重定向(在.then内),如下所示:

\n\n
loginClicked() {\n    try {\n      this.props.loginUser(this.refs.email.value, this.refs.password.value)\n      .then(() => {\n          this.props.history.push(\'/home\');       \n      });\n    }\n    catch(ex){\n      this.state.errorMsg = "Unable to connect to server";\n      console.log("error", ex);\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

2-或者另一个选项是将检查放在渲染方法中,每当存储发生任何更改时,它都会重新渲染组件,如果您发现重定向success == true到主页,如下所示:

\n\n
render(){\n\n    if(this.props.success)\n        return <Redirect to="/home" />\n\n    return (....)\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

如果您遵循第一种方法,那么您还需要将检查放在componentDidMount方法内部,如果用户想在成功登录后打开登录页面,则重定向到主页。您通过 bool 检查该 bool 值来维护登录会话success

\n\n
componentDidMount(){\n   if(this.props.success) {\n      this.props.history.push(\'/home\');\n   }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

建议

\n\n

根据DOC ,避免使用字符串引用:

\n\n
\n

如果您以前使用过 React,您可能会熟悉较旧的 API,其中 ref 属性是字符串,如“textInput”,并且 DOM 节点通过 this.refs.textInput 进行访问。我们建议不要这样做,因为字符串引用存在一些问题,被视为遗留问题,并且可能会在未来的版本之一中被删除。如果您\xe2\x80\x99 当前正在使用\n this.refs.textInput 来访问引用,我们建议\n 使用回调模式。

\n
\n